我正在尝试使用ajax将List传递给控制器方法。例如,我是列表中的2个对象,如下所示。我在控制器中获得两个对象,但内部属性为null
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)};
仍然无效。可能是什么问题?
在传递值之前的控制台
答案 0 :(得分:0)
您的控制器需要一个List,但您要传递一个具有列表属性的对象。尝试直接发送数组,它应映射到List
CustomAjaxRequest("Post", "@Url.Action("UpdateOrderHoldingsForSections", "Renewal")" ,
sectionsOrder, "json", "UpdateSectionsViewWithLatestOrderHoldings",
null, true);
或者您可以改为添加具有属性public List<OrderHoldings> Sections { get; set; }
答案 1 :(得分:0)
我将如何做:
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);
.
.
.
}
是的,请确保您正在接受控制器中的字符串,如上所示,而不是List