javascript - Receive a complex object from jquery in mvc controller -
i trying submit object form mvc controller. here 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 fetches al necessary info , builds object "model" , posts controller.
here view model:
//for receiving form public class submituserrolesviewmodel { public string userid { get; set; } public list<string> roleslist { get; set; } }
here 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 receive json object in controller? model null when post executed jquery. means not binding correctly. sure there small wrong here.
could please point out going wrong.
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); } });
Comments
Post a Comment