2016-05-12 2 views
-1

ajaxを使用してList to controllerメソッドを渡そうとしています。例私は以下のようにリスト内の2つのオブジェクトです。私は、コントローラ内の2つのオブジェクトを取得していますが、内部の性質はajax呼び出しを使用してコントローラ内のオブジェクトのリストを渡します。

var dataObject = { 'sections': sectionsOrder}; 
     console.log(dataObject); 
    CustomAjaxRequest("Post", "@Url.Action("UpdateOrderHoldingsForSections", "Renewal")" , 
     dataObject, "json", "UpdateSectionsViewWithLatestOrderHoldings", 
       null, true); 


    [HttpPost] 
    public ActionResult UpdateOrderHoldingsForSections(List<OrderHoldings> sections) 
    { 
     return null; 
    } 

nullでも、私は何も作業はまだvar dataObject = { 'sections': json.stringify(sectionsOrder)};を試しています。何が問題なの?コンソールで

値に

enter image description here enter image description here

enter image description here

+0

フルモデル(jsとC#)がなければ、何が起きているのかを伝えるのは非常に難しく、コードに追加するのは非常に難しいです。 – Gusman

+0

あなたの 'CustomAjaxRequest()'メソッドは何ですか?あなたのajaxオプションは、 'data:JSON.stringify(dataObject)'、 'contentType: 'application/jsonである必要があります。 charset = utf-8 '、' –

答えて

0

を渡す前に、お使いのコントローラがリストを期待されていますが、リストであるプロパティを持つオブジェクトを渡しています。直接配列を送信してみてください、それは

CustomAjaxRequest("Post", "@Url.Action("UpdateOrderHoldingsForSections", "Renewal")" , 
    sectionsOrder, "json", "UpdateSectionsViewWithLatestOrderHoldings", 
      null, true); 

の一覧を表示するためにマップする必要がありますまたはあなたが代わりにここでpublic List<OrderHoldings> Sections { get; set; }

0

は私がどうなるかであるという性質を持っているC#の結合モデルを追加することができます。

var jsonData = JSON.stringify(sectionsOrder);    
var dataObject = { sections: jsonData };    
CustomAjaxRequest("Post", "@Url.Action("UpdateOrderHoldingsForSections", "Renewal")" , dataObject, "json", "UpdateSectionsViewWithLatestOrderHoldings", null, true); 

そしてコントローラで、

[HttpPost] 
    public ActionResult UpdateOrderHoldingsForSections(string sections) 
    { 
     List<OrderHoldings> sectionsHoldings; 
     JavaScriptSerializer seriliazer = new JavaScriptSerializer(); 
     sectionsHoldings = seriliazer.Deserialize<List<OrderHoldings>>(sections); 
     . 
     . 
     . 
    } 

とはい、あなたはSTRを受け入れていることを確認してください上記のようなコントローラではなく、リストに表示されます

+0

あなたはモデルバインドを完全にバイパスしているので、モデル検証のメリットはありません – simonlchilds

関連する問題