Receive a complex object from jquery in mvc controller

醉酒当歌 提交于 2019-12-24 15:53:33

问题


I am trying to submit a object from a form to my mvc controller. here is the js:

<script>
    function submitForm() {
    var usersRoles = new Array;
    jQuery("#dualSelectRoles2 option").each(function () {
        usersRoles.push(jQuery(this).val());
    });
    var model = new Object();
    model.user = jQuery('#selectUser').val();
    model.roleslist = usersRoles;
    console.log('model: ' + 'user: ' + model.user + 'roles: ' + model.roleslist);
    console.log('JSON: ' + JSON.stringify(model));
    jQuery.ajax({
        type: "POST",
        url: "@Url.Action("
        AddUser ")",
        dataType: "json",
        //data: { model: JSON.stringify(usersRoles) },
        data: {
            model: JSON.stringify(model)
        },
        success: function (data) {
            alert(data);
        },
        failure: function (errMsg) {
            alert(errMsg);
        }
    });
}

now that fetches al the necessary info and builds the object "model" and then posts it to the controller.

Here is my view model:

//for receiving from form
public class submitUserRolesViewModel
{
    public string userId { get; set; }

    public List<String> rolesList { get; set; }
}

Here is my controller:

 //Post/ Roles/AddUser
    [HttpPost]       
    public ActionResult AddUser(StrsubmitUserRolesViewModel model)
    {
        if (model != null)
        {          

            return Json("Success");
        }
        else
        {
            return Json("An Error Has occoured");
        }
    }

How can I receive a json object in the controller? Now currently my model is null when a post is executed by the jquery. This means that it is not binding correctly. I am sure there is just something small wrong here.

Could you please point out where I am going wrong.


回答1:


jQuery.ajax({
        type: "POST",
        url: "@Url.Action("AddUser")",
        contentType: "application/json",
        data: JSON.stringify(model),
        success: function (data) { alert(data); },
        error: function (errMsg) {
            alert(errMsg);
        }
    });



回答2:


I slightly modified you JQuery and it is working as expected now -

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
    function submitForm() {
        var roles = ["role1", "role2", "role3"];
        var rolesArray = jQuery.makeArray(roles);

        var model = new Object();
        model.userId = 1;
        model.rolesList = rolesArray;

        jQuery.ajax({
            type: "POST",
            url: "@Url.Action("AddUser")",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(model),
            success: function (data) { alert(data); },
            failure: function (errMsg) {
                alert(errMsg);
            }
        });
    }
</script>

<input type="button" value="Click" onclick="submitForm()"/>

Output -



来源:https://stackoverflow.com/questions/21595781/receive-a-complex-object-from-jquery-in-mvc-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!