Unfortunately, the dictionary has always had a problem with model binding in MVC. Read the full story here . Therefore, we must create our own own connecting device in order to get the dictionary as a parameter for the action of our controller.
To solve your requirement, here is a working solution -
First create your ViewModels as follows. PersonModel may have a list of RoleModels.
public class PersonModel { public List<RoleModel> Roles { get; set; } public string Name { get; set; } } public class RoleModel { public string RoleName { get; set;} public string Description { get; set;} }
Then do an index action that will serve the main index -
public ActionResult Index() { return View(); }
Index view will have the following jQuery AJAX POST operation -
<script src="~/Scripts/jquery-1.10.2.min.js"></script> <script> $(function () { $('#click1').click(function (e) { var jsonObject = { "Name" : "Rami", "Roles": [{ "RoleName": "Admin", "Description" : "Admin Role"}, { "RoleName": "User", "Description" : "User Role"}] }; $.ajax({ url: "@Url.Action("AddUser")", type: "POST", data: JSON.stringify(jsonObject), contentType: "application/json; charset=utf-8", dataType: "json", error: function (response) { alert(response.responseText); }, success: function (response) { alert(response); } }); }); }); </script> <input type="button" value="click1" id="click1" />
Messages about index actions in AddUser action -
[HttpPost] public ActionResult AddUser(PersonModel model) { if (model != null) { return Json("Success"); } else { return Json("An Error Has occoured"); } }
So, now that the message is occurring, you can get all the published data in the action model parameter.

ramiramilu Feb 05 2018-12-14T00: 00Z
source share