我使用的是EF4(Db First),并且我有一个具有许多不可为null属性的实体。
在编辑表单(Razor/MVC3)中,我希望只允许编辑其中一个属性,而不允许编辑其他属性。
为了实现这一点,我必须为其他不能为null的属性设置@Html.HiddenFor(...)
语句,否则我在SaveChanges()上会出错。
有没有一种简单的方法可以将ID隐藏在视图中,即可以编辑的属性上,然后只更新该属性?
在这种情况下,您所需要做的就是将正在编辑的实体的ID作为隐藏字段以及您实际想要编辑的属性的文本字段:
@using (Html.BeginForm())
{
@Html.HiddenFor(x => x.ID)
@Html.EditorFor(x => x.PropertyYouWannaEdit)
<button type="submit">Update</button>
}
然后在相应的控制器操作中,您可以从数据库中检索需要编辑的实体,更新需要编辑的属性的值并保存更改。
[HttpPost]
public ActionResult Update(SomeEntity model)
{
SomeEntity entityToEdit = db.GetEntity(model.ID);
entityToEdit.PropertyYouWannaEdit = model.PropertyYouWannaEdit;
db.Update(entityToEdit);
return RedirectToAction("Success");
}
但就我个人而言,我会使用视图模型和AutoMapper来处理这种情况。因此,我将从设计一个视图模型开始,该模型表示我的视图的需求,并包括只需要编辑的属性:
public class MyEntityViewModel
{
public int ID { get; set; }
public string Property1ToBeEdited { get; set; }
public string Property2ToBeEdited { get; set; }
...
}
然后有相应的视图:
@model MyEntityViewModel
@using (Html.BeginForm())
{
@Html.HiddenFor(x => x.ID)
@Html.EditorFor(x => x.Property1ToBeEdited)
@Html.EditorFor(x => x.Property2ToBeEdited)
...
<button type="submit">Update</button>
}
最后是两个控制器动作(GET和POST):
public ActionResult Update(int id)
{
// Fetch the domain model that we want to be edited from db
SomeEntity domainModel = db.GetEntity(id);
// map the domain model to a view model
MyEntityViewModel viewModel = Mapper.Map<SomeEntity, MyEntityViewModel>(domainModel);
// pass the view model to the view
return View(viewModel);
}
[HttpPost]
public ActionResult Update(MyEntityViewModel model)
{
if (!ModelState.IsValid)
{
// validation failed => redisplay view so that the user can fix the errors
return View(model);
}
// fetch the domain entity that needs to be edited:
SomeEntity entityToEdit = db.GetEntity(model.ID);
// update only the properties that were part of the view model,
// leaving the others intact
Mapper.Map<MyEntityViewModel, SomeEntity>(model, entityToEdit);
// persist the domain model
db.Update(entityToEdit);
// we are done
return RedirectToAction("Success");
}