我一直在嘗試整個下午通過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) –