将值从EditorFor传递给控制器

时间:2012-09-30 10:04:03

标签: asp.net-mvc asp.net-mvc-3

这是我的观点,其中教育是模型中的列表。

 @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 个答案:

答案 0 :(得分:1)

只有下拉列表中的选定值才会回发,因此如果验证失败(例如,如果必须重新显示View),则需要重新填充下拉列表。

您的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
}