升级到 net core 3.1 - 删除 [System.Web.Http.FromBody] 中断 ajax 响应
Upgrading to net core 3.1 - removal of [System.Web.Http.FromBody] breaks ajax response
[System.Web.Http.FromBody] 似乎不存在于 net core 中,它似乎有助于使 ajax 响应在 asp net mvc 中工作。
这是Ajax:
$(document).on('click', '#txtBanishedPersonAlias', function (e) {
e.preventDefault();
var token = $('input[name=__RequestVerificationToken]').val();
var varModel = {};
varModel.BanishedAlias = $("#txtAliasEntry").val();
varModel.BanishedPersonId = $("#BanishedPerson_Id").val();
$.ajax({
url: serverRoot + "/Webforms/SaveBanishedPersonAlias",
method: "POST",
responseType: 'json',
data: {
__RequestVerificationToken: token,
model: varModel,
},
success: function (data) {
for (var i = 0; i < data.length; i++) {
$("#InsertAliasList").append("<p class=\"tempFileListItem\" style=\"color:black\">" + data[i].BanishedAlias + "</p>");
};
console.log(JSON.stringify(data));
//alert(JSON.stringify(data));
},
error: function () {
alert("There has been an error. Please grab a screenshot and contact the LIBC Help Desk.");
}
})
});
这是控制器:
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin, AADUser")]
public async Task<ActionResult> SaveBanishedPersonAliasAsync([System.Web.Http.FromBody] BanishedPersonAlias model)
{
// Gets Logged in User's info from the Registered user.
var user = await _userManager.GetUserAsync(User);
model.SubmitterId = user.Id;
if (ModelState.IsValid)
{
if (model.Id == 0)
{
_dbcontext.BanishedPersonAlias.Add(model);
}
_dbcontext.SaveChanges();
}
// linq example for returning a single column of children.
// Remember that error occurs if selecting the virtual property of the Parent Reference.
var queryAliases =
from bpa in _dbcontext.BanishedPersonAlias
where bpa.BanishedPersonId == model.BanishedPersonId
orderby bpa.BanishedAlias ascending
//select bpa.BanishedAlias;
// Example of selecting multiple columns to pass back to the view.
select new { bpa.BanishedAlias, bpa.BanishedPersonId };
return Json(queryAliases);
}
没有 [System.Web.Http.FromBody],控制器正确地将新条目输入数据库,但页面上的响应显示为“未定义”。
不过,在检查响应时,我看到了所有正确的结果 - [{"banishedAlias":"f","banishedPersonId":1006},{"banishedAlias":"fff","banishedPersonId" :1006},{"banishedAlias":"fffe","banishedPersonId":1006},{"banishedAlias":"testalias1","banishedPersonId":1006},{"banishedAlias":"testalias2","banishedPersonId":1006 }]
but the response on the page shows as "undefined."
On inspection of the Response, though, i see all of the proper results
您需要将 data[i].BanishedAlias
更改为 data[i].banishedAlias
:
$("#InsertAliasList").append("<p class=\"tempFileListItem\" style=\"color:black\">" + data[i].banishedAlias + "</p>");
[System.Web.Http.FromBody] doesn't seem to exist in net core
FromBody
存在于 asp.net 核心的 Microsoft.AspNetCore.Mvc
命名空间中。 System.Web.Http.FromBody
在 asp.net.
[System.Web.Http.FromBody] 似乎不存在于 net core 中,它似乎有助于使 ajax 响应在 asp net mvc 中工作。
这是Ajax:
$(document).on('click', '#txtBanishedPersonAlias', function (e) {
e.preventDefault();
var token = $('input[name=__RequestVerificationToken]').val();
var varModel = {};
varModel.BanishedAlias = $("#txtAliasEntry").val();
varModel.BanishedPersonId = $("#BanishedPerson_Id").val();
$.ajax({
url: serverRoot + "/Webforms/SaveBanishedPersonAlias",
method: "POST",
responseType: 'json',
data: {
__RequestVerificationToken: token,
model: varModel,
},
success: function (data) {
for (var i = 0; i < data.length; i++) {
$("#InsertAliasList").append("<p class=\"tempFileListItem\" style=\"color:black\">" + data[i].BanishedAlias + "</p>");
};
console.log(JSON.stringify(data));
//alert(JSON.stringify(data));
},
error: function () {
alert("There has been an error. Please grab a screenshot and contact the LIBC Help Desk.");
}
})
});
这是控制器:
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin, AADUser")]
public async Task<ActionResult> SaveBanishedPersonAliasAsync([System.Web.Http.FromBody] BanishedPersonAlias model)
{
// Gets Logged in User's info from the Registered user.
var user = await _userManager.GetUserAsync(User);
model.SubmitterId = user.Id;
if (ModelState.IsValid)
{
if (model.Id == 0)
{
_dbcontext.BanishedPersonAlias.Add(model);
}
_dbcontext.SaveChanges();
}
// linq example for returning a single column of children.
// Remember that error occurs if selecting the virtual property of the Parent Reference.
var queryAliases =
from bpa in _dbcontext.BanishedPersonAlias
where bpa.BanishedPersonId == model.BanishedPersonId
orderby bpa.BanishedAlias ascending
//select bpa.BanishedAlias;
// Example of selecting multiple columns to pass back to the view.
select new { bpa.BanishedAlias, bpa.BanishedPersonId };
return Json(queryAliases);
}
没有 [System.Web.Http.FromBody],控制器正确地将新条目输入数据库,但页面上的响应显示为“未定义”。
不过,在检查响应时,我看到了所有正确的结果 - [{"banishedAlias":"f","banishedPersonId":1006},{"banishedAlias":"fff","banishedPersonId" :1006},{"banishedAlias":"fffe","banishedPersonId":1006},{"banishedAlias":"testalias1","banishedPersonId":1006},{"banishedAlias":"testalias2","banishedPersonId":1006 }]
but the response on the page shows as "undefined." On inspection of the Response, though, i see all of the proper results
您需要将 data[i].BanishedAlias
更改为 data[i].banishedAlias
:
$("#InsertAliasList").append("<p class=\"tempFileListItem\" style=\"color:black\">" + data[i].banishedAlias + "</p>");
[System.Web.Http.FromBody] doesn't seem to exist in net core
FromBody
存在于 asp.net 核心的 Microsoft.AspNetCore.Mvc
命名空间中。 System.Web.Http.FromBody
在 asp.net.