下拉列表的值不会传递回控制器

时间:2012-07-16 17:55:54

标签: asp.net-mvc

我有一个视图模型,它由患者对象和Visits集合组成。将使用所有正确的属性值填充视图模型,并在“编辑”视图中正确显示这些属性。在模型传递给控制器​​的Edit(GET)操作中的视图之前,我还验证了视图模型中的属性值。

问题是,即使我在编辑视图中没有更改任何属性,绑定到下拉列表(性别和条件)的属性也不会传递回控制器的编辑(POST)操作。这导致抛出异常。

这些属性具有值,但在某些地方它们会丢失。我该如何解决这个问题?

ASP.NET MVC 4 RC

public ActionResult Edit(int id = 0)
{
    var patient = this.db.Patients.Where(p => p.Id == id).FirstOrDefault();
    var visits = this.db.Visits.Where(v => v.PatientId == patient.Id && v.IsActive).OrderByDescending(v => v.VisitDate);

    PatientEditViewModel model = new PatientEditViewModel();
    model.Patient = patient;
    model.Visits = visits;

    if (patient == null)
    {
        return this.HttpNotFound();
    }

    ViewBag.GenderId = new SelectList(this.db.Genders, "Id", "Name", patient.GenderId);
    ViewBag.HandednessId = new SelectList(this.db.Handednesses, "Id", "Name", patient.HandednessId);

    return this.View(model);
}


[HttpPost]
public ActionResult Edit(PatientEditViewModel model)
{
    Mapper.CreateMap<PatientEditViewModel, Patient>();

    var entity = Mapper.Map<PatientEditViewModel, Patient>(model);

    if (ModelState.IsValid)
    {
        this.db.Entry(entity).State = EntityState.Modified;
        this.db.SaveChanges();
    }

    this.ViewBag.GenderId = new SelectList(this.db.Genders, "Id", "Name", entity.GenderId);
    this.ViewBag.HandednessId = new SelectList(this.db.Handednesses, "Id", "Name", entity.HandednessId);

    return this.View(model);
}

@model Web.Models.PatientEditViewModel

@{
    ViewBag.Title = "XXX";
}


@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Patient</legend>

        @Html.HiddenFor(model => model.Patient.Id)

        <div class="editor-label">
            @Html.LabelFor(model => model.Patient.GenderId, "Gender")
        </div>
        <div class="editor-field">
            @Html.DropDownFor("GenderId", String.Empty)
            @Html.ValidationMessageFor(model => model.Patient.GenderId)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Patient.HandednessId, "Handedness")
        </div>
        <div class="editor-field">
            @Html.DropDownFor("HandednessId", String.Empty)
            @Html.ValidationMessageFor(model => model.Patient.HandednessId)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Patient.Comments)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Patient.Comments)
            @Html.ValidationMessageFor(model => model.Patient.Comments)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

@Html.Partial("_CurrentAndPreviousVisits", Model.Visits)


@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")


}

1 个答案:

答案 0 :(得分:4)

没有Html.DropDownFor助手。也许你的意思是Html.DropDownListHtml.DropDownListFor。我个人会推荐你第二个和强类型版本。

因此,在PatientEditViewModel模型中,您可以定义以下属性:

public string GenderId { get; set; }
public IEnumerable<SelectList> Genders { get; set; }

public string HandednessId { get; set; }
public IEnumerable<SelectList> Handednesses { get; set; }

然后在您的控制器操作中,您忘记了ViewBag并正确填充了您的视图模型。因此,您使用2行视图模型分配替换2行ViewBag:

model.Genders = new SelectList(this.db.Genders, "Id", "Name", patient.GenderId);
model.Handednesses = new SelectList(this.db.Handednesses, "Id", "Name", patient.HandednessId);

并在您的视图中使用强类型版本的帮助程序:

@Html.DropDownListFor(x => x.GenderId, Model.Genders)

@Html.DropDownListFor(x => x.HandednessId, Model.Handednesses)

现在,在您的POST操作中,您将在视图模型的GenderIdHandednessId属性中获得2个选定值。显然,在你的POST操作中,如果你想重新显示同一个视图,你会删除2行ViewCrap(原谅我的意思是ViewBag)和2行视图模型属性赋值。