<% using (Html.BeginForm("Action", "Controller", FormMethod.Post))
{%>
<%: Html.AntiForgeryToken()%>
<%: Html.HiddenFor(m=>m.Id) %>
<% for (int i = 0; i < Model.CollectionOfItems.Count; i++)
{%>
<%: Html.HiddenFor(m =>Model.CollectionOfItems.ToList()[i].Id)%>
<%: Html.HiddenFor(m =>Model.CollectionOfItems.ToList()[i].Name)%>
<%: Html.EditorFor(m => Model.CollectionOfItems.ToList()[i].NumbersToState)%>
<%} %>
<input type="submit" value="submit"/>
<%} %>
問題是當我發佈模型到控制器操作。 「CollectionOfItems」的計數爲0.模型狀態有效,沒有編譯錯誤。MVC post模型 - 收藏屬性沒有元素
編輯1:我收到屬性模型 - Id。 編輯2:控制器動作
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Action(Model ReceivedModel)
{
try
{
//here i have break point to check value of ReceivedModel
// here i can read the value id of the received model - for ex.: 5
}
catch (Exception)
{
return ""
}
}
編輯3: 模型是實體從ADO實體數據模型genereted
public partial class Model
{
public Model()
{
this.CollectionOfItems= new HashSet<Items>();
}
public int Id{ get; set; }
public virtual ICollection<Items> CollectionOfItems{ get; set; }
}
編輯4:
這是從foreach循環的結果
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." name="[0].Id" type="hidden" value="21">
你可以在你創建模型的地方顯示控制器方法嗎? – Andrei
請發佈控制器代碼! – Fals
我發佈我的編輯 – user2831001