2016-05-12 40 views
-1

我想通过列表控制器方法使用ajax。例如我在下面的列表中有2个对象。我得到的控制器的两个对象,但里面性能null传递控制器中使用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; 
    } 

即使我尝试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

相关问题