我使用以下代码调用控制器方法并传递参数。 调用方法但控制器方法中的值为空。
相同的代码在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; }
}
答案 0 :(得分:1)
你可以这样试试
var UserModel =
{
FName: "Abc",
LName: "Pqr"
};
$.ajax({
url: '/Home/AddUser/',
type: "POST",
//contentType: 'application/json; charset=utf-8',
dataType: "json",
data: UserModel,
success: function () {
}
});
答案 1 :(得分:0)
请删除JSON.stringify
var UserModel = {
"FName": "Abc",
"LName": "Pqr"
};
$.ajax({
url: 'http://localhost:60600/Home/AddUser/',
type: "POST",
contentType: 'application/json; charset=utf-8',
dataType: "json",
data: UserModel,
success: function () {
}
});