2012-09-30 162 views
2

這是我的觀點,教育是模型中的列表。將EditorFor的值傳遞給控制器​​

@using chpayroll.Models.CustInformations 
@model CustInfoExtract 

@Html.HiddenFor(x => x.flag, new { @id = "flag" }) 
     @Html.HiddenFor(x => x.StaffId) 
     <table style=" width:730px">  
      <tr> 
       <th>Country</th> 
       <th>Board</th> 
       <th>Level</th> 
       <th>PassedYear</th> 
       <th>Division</th> 
      </tr>  
      <tr> 
       @Html.EditorFor(x => x.education) 
      </tr> 
      <tr> 
       <td><input type="submit" value="Add Another" id="addedu"/> </td> 
      </tr> 
     </table> 

我有編輯模板如下

@using staffInfoDetails.Models 
@model staffInfo.education 

@Html.HiddenFor(x=>x.staffId) 

<tr> 
    <td >@Html.DropDownListFor(x => x.country, Model.countryList, "--select--", new { @id="country"})</td> 
    <td>@Html.TextBoxFor(x => x.board, new { @id="board"})</td> 
    <td>@Html.TextBoxFor(x => x.level, new { @id="level"})</td> 
    <td>@Html.TextBoxFor(x => x.passedYr, new { @id="passedYr"})</td> 
    <td>@Html.DropDownListFor(x => x.passedDiv, Model.passedDivList, "--select--", new { @id="division"})</td> 
</tr> 

我試圖通過從控制器模型查看並從視圖控制器回來。當我通過模型查看時,教育列表已通過,但是,當我試圖將模型從視圖傳遞給控制器​​時,除了教育列表外,其他所有內容都通過了。我怎麼解決這個問題 ?

回答

1

只有從下拉列表中選擇的值將被回發,因此如果驗證失敗(即,如果必須重新顯示視圖),則需要重新填充下拉列表。

你的POST動作看起來可能沿着以下線的東西:

[HttpPost] 
public ActionResult Home(CustInformations viewModel) 
{ 
    if (!ModelState.IsValid) 
    { 
     // Re-populate drop-down list and redisplay form 
     viewModel.DropdownListOptions = _repository.getEductionList(); 
     return View(viewModel); 
    } 

    // Validation passed 
    // Save, update, etc and redirect to new page 
}