2

我有一个由患者对象和访问集合组成的视图模型。视图模型填充了所有正确的属性值,并且属性在编辑视图中正确显示。在将模型传递给控制器​​的编辑 (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")


}
4

1 回答 1

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并正确填充您的视图模型。因此,您将 ViewBag 的 2 行替换为 2 行视图模型分配:

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 操作中,您将在视图模型的GenderId和属性中获得 2 个选定的值。HandednessId显然,在您的 POST 操作中,如果您想重新显示相同的视图,您可以使用 2 行视图模型属性分配删除 2 行 ViewCrap(请原谅我的意思是 ViewBag)。

于 2012-07-16T20:44:15.403 回答