Receive a complex object from jquery in mvc contro

2019-07-24 11:10发布

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.

2条回答
beautiful°
2楼-- · 2019-07-24 11:42
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);
        }
    });
查看更多
贪生不怕死
3楼-- · 2019-07-24 11:46

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 -

enter image description here

查看更多
登录 后发表回答