[英]How to send model binded to view from ajax call on button click to controller action?
這是我的ajax電話:
$('#addNominationForm').on("submit", function (e) {
debugger;
e.preventDefault();
$.ajax({
type: 'post',
url: '@Url.Action("AddNomination", "Nomination")',
data: JSON.stringify({ model: model, submit: "submit" }),
contentType: "application/json; charset=utf-8",
success: function (data) {
swal({
title: "Nominated Successfully",
showCancelButton: false,
confirmButtonColor: "#88a364",
confirmButtonText: "Ok"
},
function () {
if (isConfirm) {
window.location.href = '@Url.Action("Dashboard", "Dashboard")';
}
});
},
Error: function (xhr, status, error) {
alert("error" + xhr.responseText)
}
});
});
這是動作:
[HttpPost]
[CustomeAuthorize(AllowedRole = "Manager")]
public ActionResult AddNomination(NominationViewModel model, string submit)
{
//business logic in action
}
但是在這里,當ajax調用完成時,實際模型中將包含null。 我該怎么辦?或者還有其他方法可以通過在普通按鈕單擊時使用ajax調用在視圖上發送模型嗎?
不要對您的數據進行分類:
data: { model: model, submit: "submit" }
POST的主體為JSON(在這種情況下)
GET可以從查詢字符串中獲取數據,因此您需要在其中進行字符串化
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.