Blu*_*ppy 3 entity-framework-4 asp.net-mvc-3
我正在使用EF4(Db First),我有一个具有许多非可空属性的实体.
在编辑表单(Razor/MVC3)中,我想只允许编辑其中一个属性,而不允许编辑其他属性.
为了使其工作,我不得不为@Html.HiddenFor(...)我的其他每个属性放置无法为空的语句,否则我在SaveChanges()上收到错误.
是否有一种简单的方法可以在视图上隐藏ID,可以编辑的属性,然后只更新该属性?
在这种情况下,您需要做的就是将您正在编辑的实体的ID包含为隐藏字段以及您实际想要编辑的属性的文本字段:
@using (Html.BeginForm())
{
@Html.HiddenFor(x => x.ID)
@Html.EditorFor(x => x.PropertyYouWannaEdit)
<button type="submit">Update</button>
}
Run Code Online (Sandbox Code Playgroud)
然后在相应的控制器操作中,您可以从数据库中检索需要编辑的实体,更新需要编辑的属性的值并保存更改.
[HttpPost]
public ActionResult Update(SomeEntity model)
{
SomeEntity entityToEdit = db.GetEntity(model.ID);
entityToEdit.PropertyYouWannaEdit = model.PropertyYouWannaEdit;
db.Update(entityToEdit);
return RedirectToAction("Success");
}
Run Code Online (Sandbox Code Playgroud)
但我个人会使用视图模型和AutoMapper来处理这种情况.所以我首先要设计一个表示我的视图要求的视图模型,并包括仅需要编辑的属性:
public class MyEntityViewModel
{
public int ID { get; set; }
public string Property1ToBeEdited { get; set; }
public string Property2ToBeEdited { get; set; }
...
}
Run Code Online (Sandbox Code Playgroud)
然后有相应的视图:
@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>
}
Run Code Online (Sandbox Code Playgroud)
最后是2个控制器动作(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");
}
Run Code Online (Sandbox Code Playgroud)