Sending Multiple Items to MVC Controller via jQuery.Ajax

后端 未结 3 1846
北海茫月
北海茫月 2021-01-03 00:10

If youa are serializing a form using something like jQuery, it will often map the JSON keys and values to the properties of a object on the Controller Action you are posting

3条回答
  •  轮回少年
    2021-01-03 00:52

    Another solution if you want a dictionary of key/value pairs:

    public void TestMVC(MyObject obj, IDictionary TheWeirdQueryString)
    {
    }
    

    Client:

    function PostForm() {
        $.ajax({
            url: "/Home/TestMVC",
            type: "POST",
            dataType: "application/JSON",
            data: $('#form').serialize() + "&" + $('.additionalParams').serialize()
        });
    };
    

    $('.additionalParams').serialize() format:

    TheWeirdQueryString[0].Key=param0&TheWeirdQueryString[0].Value=value0&TheWeirdQueryString[1].Key=param1&TheWeirdQueryString[1].Value=value1&...&TheWeirdQueryString[n].Key=paramN&TheWeirdQueryString[n].Value=valueN
    

    UPDATED:

    You need something like this:

    
    
    
    
    
    

提交回复
热议问题