【发布时间】:2018-12-07 10:54:12
【问题描述】:
我知道这是一个在开发者论坛上经常被问到的问题,但尽管进行了搜索,但我找不到卡住的问题。
我只是想通过 Ajax POST 将一些值传递给我的控制器。 Ajax 接收到值,但它们在我的控制器中到达 null。
我尝试声明一个具有相同变量名的模型,我还尝试将每个字段放入我的方法的签名中(具有相同的变量名),但结果始终相同:null。
这是否可能因为我的视图模型与我想要接收数据的模型不同而不起作用?
您是否知道卡住了什么,尤其是它的原因是什么?
谢谢!
控制器 V1
[Authorize]
[HttpPost]
public async Task<ActionResult> ShowRegistration(Models.RegisterForm rF)
{
控制器 V2
[Authorize]
[HttpPost]
public async Task<ActionResult> ShowRegistration(string Id, string Status, string Checkin, string Checkout, string Cost, bool Terms, bool Info, string Member_Zkp)
{
return View();
}
型号
public class RegisterForm
{
public string Id { get; set; }
public string Status { get; set; }
public string Checkin { get; set; }
public string Checkout { get; set; }
public string Cost { get; set; }
public bool Terms { get; set; }
public int TermsINT { get; set; }
public int InfoINT { get; set; }
public bool Info { get; set; }
public string Member_Zkp { get; set; }
}
Ajax 函数
function Edit() {
event.preventDefault();
var id = $('#Zkp').val();
var status = $('#StatusDrop').val();
var cost = $('#Cost').val();
var checkin = $('#Checkin').val();
var checkout = $('#Checkout').val();
var check1 = $('#check1').val();
var check2 = $('#check2').val();
var dataRegister = { "Id": id, "Status": status, "Cost": cost, "Terms": check1, "Info": check2 };
$('html, body').animate({ scrollTop: 0 }, 'fast');
$('#alertWait').show('fade');
setTimeout(function () {
$('#alertWait').hide('fade');
}, 4000);
$.ajax({
url: "https://localhost:44338/Registration/ShowRegistration/",
data: { "Id" : id, "Status": status, "Cost": cost, "Terms": check1, "Info": check2 },
type: 'POST',
contentType: 'application/JSON',
success: function (data) {
if (data === "success") {
$('html, body').animate({ scrollTop: 0 }, 'fast');
$('#alertOk').show('fade');
setTimeout(function () {
$('#alertOk').hide('fade');
}, 4000);
}
},
error: function () {
$('html, body').animate({ scrollTop: 0 }, 'fast');
$('#alertError').show('fade');
setTimeout(function () {
$('#alertError').hide('fade');
}, 4000);
}
});
}
【问题讨论】:
-
您在发帖时是否获得授权?
-
是的,我已经登录并输入了方法,但我的所有值都是空的。
-
尝试从帖子数据中的每个键中删除双引号,例如 =>
data: { Id : id, Status: status, Cost: cost, Terms: check1, Info: check2 } -
或者你可以使用
data: $('#formid').serialize() -
尝试隐藏这一行 =>
contentType: 'application/JSON',你的 ajax 调用在我身边工作,除了这一行
标签: javascript c# asp.net ajax asp.net-mvc