C# 如何从MVC3中的编辑页面更新视图模型?
我有三个模型一起创建一个视图模型,我希望在单击“编辑”时能够编辑该视图模型。我找不到一个直接的例子来说明这是如何工作的(在任何地方) 我不确定我是否走上了正确的道路。我能够用数据获得视图。在这一点上,我无法保存它 任何帮助都将不胜感激 谢谢 型号:C# 如何从MVC3中的编辑页面更新视图模型?,c#,asp.net,asp.net-mvc-3,viewmodel,C#,Asp.net,Asp.net Mvc 3,Viewmodel,我有三个模型一起创建一个视图模型,我希望在单击“编辑”时能够编辑该视图模型。我找不到一个直接的例子来说明这是如何工作的(在任何地方) 我不确定我是否走上了正确的道路。我能够用数据获得视图。在这一点上,我无法保存它 任何帮助都将不胜感激 谢谢 型号: public class Person { [Key] public int Id { get; set; } [MaxLength(20)] [Required(ErrorMessage = "First
public class Person
{
[Key]
public int Id { get; set; }
[MaxLength(20)]
[Required(ErrorMessage = "First name is required.")]
public string FirstName { get; set; }
[MaxLength(20)]
[Required(ErrorMessage = "Last name is required.")]
public string LastName { get; set; }
[MaxLength(40)]
[Required(ErrorMessage = "Email is required.")]
public string Email { get; set; }
[MaxLength(20)]
[DataType(DataType.PhoneNumber)]
public string Phone { get; set; }
public bool Active { get; set; }
}
public class ClientContact
{
[Key]
[ForeignKey("Person")]
public int ClientPersonId { get; set; }
public int ClientId { get; set; }
[MaxLength(40)]
public string Title { get; set; }
public Person Person { get; set; }
[ForeignKey("ClientId")]
public Client Client { get; set; }
}
public class Client
{
[Key]
public int ClientId { get; set; }
public string Name { get; set; }
public bool Active {get;set;}
}
视图模型:
public class ClientContactViewModel
{
private SimplexDB db = new SimplexDB();
public ClientContactViewModel()
{
}
public ClientContactViewModel(int id)
{
ClientPersonId = id;
InitializeClientContact();
}
public int ClientPersonId { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = " Last Name")]
public string LastName { get; set; }
[Display(Name = "Title")]
public string Title { get; set; }
[Display(Name = "Email Address")]
public string Email { get; set; }
[Display(Name = "Phone")]
public string Phone { get; set; }
[Display(Name = "Client Name")]
public int ClientId { get; set; }
public SelectList Clients
{
get
{
return new SelectList(db.Clients, "ClientId", "Name");
}
}
private void InitializeClientContact()
{
var contact = db.ClientPersons.Include("Person").Where(x => x.ClientPersonId == ClientPersonId).SingleOrDefault();
if (contact != null)
{
FirstName = contact.Person.FirstName;
LastName = contact.Person.LastName;
Title = contact.Title;
Email = contact.Person.Email;
Phone = contact.Person.Phone;
ClientId = contact.ClientId;
}
}
}
控制器:
public class ClientContactController : Controller
{
private database db = new database();
//
// GET: /ClientContact/Edit/5
public ActionResult Edit(int id)
{
return View(new ClientContactViewModel(id));
}
//
// POST: /ClientContact/Edit/5
[HttpPost]
public ActionResult Edit(ClientContactViewModel model)
{
if (ModelState.IsValid)
{
db.Entry(model).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
}
我在db.Entry(model.State)中得到一个错误。。。“实体类型ClientContactViewModel不是当前上下文模型的一部分。”您的ViewModel不是实体。您应该将ViewModel映射到实体,然后将实体的状态设置为“已修改” 基本上,这意味着您应该使用视图模型值设置实体值。您可以手动使用或处理它:
[HttpPost]
public ActionResult Edit(ClientContactViewModel model)
{
if (ModelState.IsValid)
{
ClientContact contact = db.ClientPersons.Include("Person")
.Where(x => x.ClientPersonId == model.ClientPersonId)
.SingleOrDefault();
contact.FirstName = model.FirstName;
// etc
db.Entry(contact).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
有关在MVC中使用ViewModels的优秀方法,请参见
另外,我强烈建议不要在ViewModel中进行任何数据访问。在控制器中执行此操作,或者更好地在控制器使用的存储库中执行此操作。模型绑定不能很好地处理具有逻辑的模型(即它们不应包含任何内容,而只包含简单的get/set属性)。您需要在get操作中将模型的属性移动到viewmodel。在POST操作中,从db获取原始模型,使用视图模型中的数据更新模型,然后保存更改。模型本质上是数据库中表的表示形式。您的视图模型正是屏幕上显示的
[HttpPost]
public ActionResult Edit(ClientContactViewModel model)
{
if (ModelState.IsValid)
{
var client = db.Client.Where(c => c.Id = model.ClientPersonId);
client.FirstName = model.FirstName;
...etc through all your properties and other models...
db.Entry(model).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
有更巧妙的方法可以做到这一点,但这代表了一个没有抽象概念的想法。我该怎么做呢?很抱歉,我是新来的。说清楚,我是否需要根据您上面编写的代码更新我的视图模型?谢谢你的帮助。没问题,很高兴我能帮忙。