我是编程新手,我想将表输入值传递给控制器。我试过这个:
$("#btnsend").click(function () {
$.ajax({
type: "POST",
contentType: "application/json ; charset=utf-8",
data: {
buyerID: $('.BuyerID').val(),
},
url: "/SaveReservation",
success: function (data) {
alert('buyerID : ' + data);
},
error: function (result) {
alert('something went wrong');
}
})
});
控制器是这样的:
[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult SaveReservation(BuyerModel buyer)
{
return Json(buyer.DistibutorID);
}
当我单击按钮时,我获得了成功状态,但在警报中,我在单词buyerID
之后获得了项目的所有源代码。
答案 0 :(得分:0)
你可能想试试这个:
return Json(new { success = true, message = buyer.DistibutorID },
JsonRequestBehavior.AllowGet);
和
alert('buyerID : ' + data.message );
答案 1 :(得分:0)
请注意,这是在您的脚本中
data: {
buyerID: $('.BuyerID').val(),
},
要么必须与BuyerModel Here
具有相同的结构public JsonResult SaveReservation(BuyerModel buyer) <- BuyerModel that you are using
{
return Json(buyer.DistibutorID);
}
或者你可以只改变你的控制器只接受一个参数像这样
public JsonResult SaveReservation(string buyerID) <- Follow the same Object structure you are passing in Ajax
{
return Json(buyer.DistibutorID);
}