我使用以下代码来调用 Controller 方法并传递参数。 该方法正在被调用,但 Controller 方法中的值变为空。
相同的代码在 MVC 4 项目中运行良好,但在 Asp.Net Core MVC 中则无法运行。
var UserModel = {
"FName": "Abc",
"LName": "Pqr"
};
$.ajax({
url: 'http://localhost:60600/Home/AddUser/',
type: "POST",
contentType: 'application/json; charset=utf-8',
dataType: "json",
data: JSON.stringify(UserModel),
success: function () {
}
});
public class HomeController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
return View();
}
// [HttpPost]
// [AllowAnonymous]
public IActionResult AddUser(UserModel model)
{
// Add user model
return Json("");
}
}
public class UserModel
{
public string FName { get; set; }
public string LName { get; set; }
}
最佳答案
你可以这样尝试
var UserModel =
{
FName: "Abc",
LName: "Pqr"
};
$.ajax({
url: '/Home/AddUser/',
type: "POST",
//contentType: 'application/json; charset=utf-8',
dataType: "json",
data: UserModel,
success: function () {
}
});
关于Jquery ajax 不将参数传递给 Asp.Net Core MVC 中的 Controller 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43388190/