我一直在尝试整个下午通过Web爬行尝试接收操作控制器中的JSON对象。如何接收JSON作为MVC 5操作方法参数
什么是正确或更简单的方法去做呢?
我曾尝试以下: 1:
//Post/ Roles/AddUser
[HttpPost]
public ActionResult AddUser(String model)
{
if(model != null)
{
return Json("Success");
}else
{
return Json("An Error Has occoured");
}
}
这给了我在我输入一个空值。
2:
//Post/ Roles/AddUser
[HttpPost]
public ActionResult AddUser(IDictionary<string, object> model)
{
if(model != null)
{
return Json("Success");
}else
{
return Json("An Error Has occoured");
}
}
,给了我一个500错误jQuery开发方面正试图张贴到了吗? (意思是它没有正确绑定)。
这里是我的jQuery代码:
<script>
function submitForm() {
var usersRoles = new Array;
jQuery("#dualSelectRoles2 option").each(function() {
usersRoles.push(jQuery(this).val());
});
console.log(usersRoles);
jQuery.ajax({
type: "POST",
url: "@Url.Action("AddUser")",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(usersRoles),
success: function (data) { alert(data); },
failure: function (errMsg) {
alert(errMsg);
}
});
}
所有我想要做的就是接受我的JSON对象在我的MVC的行动?
虽然这指定MVC5,解决方案是相同的,所以这是[将JSON数据发布到ASP.NET MVC]的副本(http://stackoverflow.com/questions/ 4164114/posting-json-data-to-asp-net-mvc) –