2012-09-30 3 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 
} 
관련 문제