2017-05-30 9 views
1

Asp.Net MVCでAjaxによるページ入力を送信します。Ajaxがコントローラのアクションにヌルパラメータを送信します。

JQuery Ajax jsonデータはnullではありませんが(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"}} 

多くの回答を確認しましたが、問題を把握できませんでした。 ありがとうございます。

+0

'console.log(JSON.stringify(xy));'の出力は何ですか? – Sandman

+0

'AddProduct'メソッドにはどのようなパラメータがありますか? 'data'や' CategoryID'など? –

+0

私の質問が更新されました。 AddProductアスペクトのクラスパラメータをajaxの文字列jsonデータとして返します – Zeynep

答えて

0

はあなたがでデータを送っているキーの名前を提供する必要があり、これを試してみてください:。

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

私も手動でデータを文字列化することはあなたのために不要な追加作業であることを示唆していると思います。それはあなたのC#コードでモデルを作成するために、より多くの意味を行い、その後、あなたのためのModelBinderの仕事を聞かせ:あなたはすべてのparamsを宣言する必要があるアクションで

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... 
} 
+0

あなたの答えに「無効なjsonプリミティブ:データ」が表示されますInterval Error 500 – Zeynep

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) 

      } 
     }); 
0
data: new{data = JSON.stringify(xy)} 

このようにデータを渡す

関連する問題