2016-09-06 40 views
-2

控制器上的按鈕後訪問列表:保存列表值回,它被清除,當我嘗試每次點擊MVC

​​

型號:

public int EmployeeID { get; set; } 
[Required(ErrorMessage = "Please Enter Skill")] 
public string SkillName { get; set; } 
[Display(Name = "Experience (In Months)")] 
[Required(ErrorMessage = "Please Enter Experience(In Months)")] 
public Nullable<byte> ExperienceInMonths { get; set; } 
public string CreatedBy { get; set; } 
public Nullable<System.DateTime> CreatedDate { get; set; } 
public string ModifiedBy { get; set; } 
public Nullable<System.DateTime> ModifiedDate { get; set; } 
public Nullable<bool> IsActive { get; set; } 

public virtual EmployeeDetail EmployeeDetail { get; set; } 

查看:

model IEnumerable<CISSEC.Models.EmployeeSkill> 
<table class="table"> 
    <tr> 
     <th> @Html.DisplayNameFor(model => model.SkillName) </th> 
     <th> @Html.DisplayNameFor(model => model.ExperienceInMonths)</th> 
    </tr> 
    @foreach (var item in Model) { 
     <tr> 
      <td> @Html.DisplayFor(modelItem => item.SkillName) </td> 
      <td> @Html.DisplayFor(modelItem => item.ExperienceInMonths) </td> 
    </tr> 
    } 
</table> 
+0

請向我們展示如何將列表渲染到您要提交的'

'。 –

+0

{模型的IEnumerable <表類= 「表」> @ Html.DisplayNameFor(型號=> model.SkillName) @ Html.DisplayNameFor(型號= > model.ExperienceInMonths)在模型 @foreach(VAR項) { ​​ @ Html.DisplayFor(modelItem => item.SkillName) ​​ @ Html.DisplayFor(modelItem => item.ExperienceInMonths) } – Kcruzz

回答

0

由於您沒有爲列表項的屬性渲染<input>元素,因此您的列表未被回發。使用HiddenFor來渲染輸入。使用for循環,以便模型綁定器接收列表項的正確索引。

@for (var i = 0; i < Model.Count(); i++) { 
     <tr> 
      <td> @Html.HiddenFor(m => m[i].SkillName) </td> 
      <td> @Html.HiddenFor(m=> m[i].ExperienceInMonths) </td> 
    </tr> 
} 
相關問題