【问题标题】:Ajax sends null parameter to controller actionAjax 向控制器操作发送空参数
【发布时间】:2017-10-31 02:00:40
【问题描述】:

我想在 Asp.Net MVC 中通过 Ajax 提交页面输入。

JQuery Ajax json 数据不为空(在 Console.log() 中检查),但它将 json 字符串 null 传递给控制器​​操作。控制器动作将对象视为字符串:

类:

 public int ID { get; set; }
 public string ProductName { get; set; }
 public int CategoryID { get; set; }
 public int BrandID { get; set; }
 public int Price1 { get; set; }
 public string Exchange { get; set; }
 public bool State { get; set; }

控制器:

  [HttpPost]
    public ActionResult AddProduct(string data)
    {
         //string data comes null here
    }

jQuery:

   var xy ={
             "data": { 
                 CategoryID: categoryID,
                 BrandID: brandID,
                 ProductName: productName,
                 Price1: price1,
                 ExchangeName: exchangeName,
                 State: state
             }
         };
         console.log(JSON.stringify(xy))

        $.ajax({
            url: "/Products/AddProduct/",
            type: 'POST',
             data: JSON.stringify(xy),          
            async: true,
            contentType: "application/json; charset=utf-8",
            dataType: "json",         
            success: function (data) {

            },
            error: function (xhr, status, error) {
                alert(xhr.responseText)

            }
        });

console.log(JSON.stringify(xy)) 的输出:

 {"data":{"CategoryID":"63","BrandID":"1","ProductName":"pname","Price1":"199","State":"1"}}

我检查了很多答案,但无法弄清楚我的问题。 感谢您的帮助。

【问题讨论】:

  • console.log(JSON.stringify(xy)); 的输出是什么?
  • AddProduct 方法中有哪些参数? dataCategoryID等?
  • 我更新了我的问题。将产品方面类参数添加为来自 ajax 的字符串 json 数据

标签: jquery json ajax asp.net-mvc


【解决方案1】:

在你需要声明所有参数的动作中:

[HttpPost]
public ActionResult AddProduct(int CategoryID, int BrandID, string ProductName, double Price1, string ExchangeName, int State)
{
}

并像这样传递数据:

$.ajax({
            url: "/Products/AddProduct/",
            type: 'POST',
             data: {CategoryID: categoryID,
                 BrandID: brandID,
                 ProductName: productName,
                 Price1: price1,
                 ExchangeName: exchangeName,
                 State: state},          
            async: true,
            contentType: "application/json; charset=utf-8",
            dataType: "json",         
            success: function (data) {

            },
            error: function (xhr, status, error) {
                alert(xhr.responseText)

            }
        });

【讨论】:

    【解决方案2】:

    您需要提供用于发送数据的密钥的名称。试试这个:

    data: { data: JSON.stringify(xy.data) },       
    

    我还建议手动对数据进行字符串化对您来说是不必要的额外工作。在 C# 代码中创建模型然后让 ModelBinder 为您工作会更有意义:

    data: xy.data,
    
    // model definition:
    public class Product {
      public int CategoryID { get; set; }
      public int BrandID { get; set; }
      public string ProductName { get; set; }
      public decimal Price1 { get; set; }
      public string ExchangeName { get; set; }
      public string State { get; set; }
    }
    
    // in your controller:
    [HttpPost]
    public ActionResult AddProduct(Product product)
    {
      // work with the hydrated Product class here...
    }
    

    【讨论】:

    • 在您的回答中,我收到“无效的 json 原语:数据”间隔错误 500
    【解决方案3】:
     data: new{data = JSON.stringify(xy)}
    

    以这种方式传递您的数据

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-01
      • 2021-02-14
      • 1970-01-01
      • 2019-08-13
      • 2020-01-24
      • 1970-01-01
      • 2013-05-03
      • 2010-12-05
      相关资源
      最近更新 更多