Jquery ajax 不将参数传递给 Asp.Net Core MVC 中的 Controller 方法

标签 jquery ajax

我使用以下代码来调用 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/

相关文章:

javascript - MVC - 单击时没有任何反应( View 中的 Jquery)

javascript - Ajax 之后单击按钮后禁用按钮 - ASP.NET MVC5

javascript - jQuery 与普通 javascript : differences between append and appendChild when building DOM tree

javascript - 为什么这个事件总是触发?

javascript - 使用快速表插件时,CKEditor 颜色按钮消失

javascript - 如何使动态创建的div可编辑

javascript - 将编辑器 html 保存到客户的计算机

javascript - 将 Ajax 错误响应显示为网页

javascript - 如何在 JSP 中使用 Ajax 保存多条记录

javascript - 更改 Bootstrap 选项卡的文本链接?