0

我有一个名为 Visit 的对象,具有以下属性:-

备注 DoctorID VisitTypeID CreatedBy Date VisitID PatientID StatusID Timestamp

在编辑视图中,用户只能编辑以下两个属性:- 注意 DoctorID 所以我在编辑视图中将其他属性添加为隐藏字段,如下所示:-

    @using (Html.BeginForm()) {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Visit</legend>
            <div class="editor-label">
                @Html.LabelFor(model => model.Note)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Note)
                @Html.ValidationMessageFor(model => model.Note)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.DoctorID)
            </div>
            <div class="editor-field">
                @Html.DropDownList("DoctorID", String.Empty)
                @Html.ValidationMessageFor(model => model.DoctorID)
            </div>

            <p>
              @Html.HiddenFor(model => model.VisitTypeID)
              @Html.HiddenFor(model => model.CreatedBy)
              @Html.HiddenFor(model => model.Date)
              @Html.HiddenFor(model => model.VisitID)
              @Html.HiddenFor(model => model.PatientID)
              @Html.HiddenFor(model => model.StatusID)
              @Html.HiddenFor(model => model.timestamp)

            <input type="submit" value="Create" />

我必须在我的编辑视图中包含所有属性,因为我将访问对象传递给我的后期编辑操作方法,如下所示:-

  [HttpPost]
            public ActionResult Edit(Visit visit)
            {
                if (!(visit.Editable(User.Identity.Name)))
                {
                    return View("NotFound");
                }
                try
                {
                    if (ModelState.IsValid)
                    {
                        repository.UpdateVisit(visit);
                        repository.Save();
                        return RedirectToAction("Index");
                    }
                }
                catch (DbUpdateConcurrencyException ex)
                {
    var entry = ex.Entries.Single();
                    var clientValues = (Visit)entry.Entity;

                    ModelState.AddModelError(string.Empty, "The record you attempted to edit "
                    + "was modified by another user after you got the original value. The "

    //code goes here

因此,我担心上述方法的原因如下:- 1. 攻击者可能会修改隐藏字段的值。2. 我无法在我的 Visit 模型类中定义 [Bind(Include = "....")]。

所以我无法决定是否应该继续使用这种方法,或者有更好的方法可以遵循

4

1 回答 1

0

您可能会重新拉动Visit控制器中的对象,然后只保留您希望填充的两个字段。这将确保只编辑这两个字段。

Visit existingVisit = /* retrieve Visit */;
existingVisit.Note = visit.Note;
existingVisit.DoctorID = visit.DoctorID;
repostistory.Update(existingVisit);
reposistory.SaveChanges();

但是,我可能会更进一步,为该特定操作创建一个视图模型,该模型仅包含您关心的字段。

于 2012-04-25T23:04:11.413 回答