【问题标题】:How can I send to json data for my controller in ajax mvc?如何在 ajax mvc 中为我的控制器发送 json 数据?
【发布时间】:2020-05-05 19:04:56
【问题描述】:

我的问题:我正在发送 JSON 数据,但我的 JSON 数据变为“空”?我不明白为什么它会为空。

我阅读了所有文档,但我是 jquery 的新手。 我错过了什么? 我需要为我的控制器发送 JSON 数据。

Index.cshtml:

var UserData = {
    "CustomerID": "99999",
    "CustomerCode": "0",
    "UserID":"127",
    "StartDate": "02/09/2019",
    "FinishDate": "03/08/2020",
    "SuccesID": "1",
    "Cash": "1",
    "ProblemID": "1",
    "PageNo":"1",
    "DataNo":"1"
};
var userDataJson = JSON.stringify(UserData);
$.ajax({
    type: "POST",
    url: "/Problems/Search",
    contentType: "application/json",
    data: userDataJson,
    success: function (msg) {
        console.log(msg);
    },
    error: function (err) {
        alert("search error");
    }
});

型号:

public class ProblemsInput
{
    public string CustomerID { get; set; }
    public string CustomerCode { get; set; }
    public string UserID { get; set; }
    public string StartDate { get; set; }
    public string FinishDate { get; set; }
    public string SuccesID { get; set; }
    public string Cash { get; set; }
    public string ProblemID { get; set; }
    public string PageNo { get; set; }
    public string DataNo { get; set; }
}

控制器:

[HttpPost]
public IActionResult Search(ProblemsInput problemsinput)
{
    return View();
}

【问题讨论】:

    标签: jquery json ajax model-view-controller


    【解决方案1】:

    你只需要添加

     data: JSON.stringify({"problemsinput":userDataJson}),
    

    而不是data: userDataJson, 这样就可以解决问题了。

    Ajax 调用将是

    var userDataJson = JSON.stringify(UserData);
         $.ajax({
             type: "POST",
             url: "/Problems/Search",
             contentType: "application/json",
             data: JSON.stringify({"problemsinput":userDataJson}),
             success: function (msg) {
                 console.log(msg);
            },
            error: function (err) {
                alert("search error");
            }
        });
    

    【讨论】:

      【解决方案2】:

      只需将FromBody 属性应用于problemsinput 参数:

      [HttpPost]
      public IActionResult Search([FromBody]ProblemsInput problemsinput) 
      { 
          return View(); 
      }
      

      【讨论】:

      • 哦,@FurkanYılmaz,如果您使用的是 ASP.NET Core,那么使用 JSON.stringify 是对的。但是您忘记添加 FromBody 属性,该属性会强制在请求正文中查找值。
      【解决方案3】:

      老兄。这个问题是你的 UserData。试试这个代码:

      var problemsinput= {
          CustomerID: '99999',
          CustomerCode: '0',
          UserID:'127',
          StartDate: '02/09/2019',
          FinishDate: '03/08/2020',
          SuccesID: '1',
          Cash: '1',
          ProblemID: '1',
          PageNo: '1',
          DataNo: '1',
      };
      
       $.ajax({
           type: "POST",
           url: "/Problems/Search",
           contentType: "application/json",
           data: {problemsinput},
           success: function (msg) {
               console.log(msg);
           },
           error: function (err) {
               alert("search error");
           }
      });
      

      如果这不起作用。在 Controller 中使用 [FromBody] 属性再试一次。希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 2018-11-26
        • 2023-03-26
        • 1970-01-01
        • 2016-07-22
        • 1970-01-01
        • 2013-12-28
        • 2014-01-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多