我有一个名为Visit的对象,其中包含以下属性: -
请注意 DoctorID VisitTypeID 由...制作 日期 VisitID PatientID StatusID 时间戳
在编辑视图中,用户只能编辑以下两个属性: - 注意 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.我无法在我的访问模型类中定义[Bind(Include =“....”)]。
所以我无法决定是否应该继续使用这种方法或者有更好的方法来跟进
答案 0 :(得分:0)
您可能会重新拉动控制器中的Visit
对象,然后仅继续填充您希望填充的两个字段。这样可以确保只编辑这两个字段。
Visit existingVisit = /* retrieve Visit */;
existingVisit.Note = visit.Note;
existingVisit.DoctorID = visit.DoctorID;
repostistory.Update(existingVisit);
reposistory.SaveChanges();
但是,我可能会更进一步,为该特定操作创建一个视图模型,该模型仅包含您关注的字段。