如何使用 asp mvc 和 nhibernate 高效更新带有引用(dropdownList)的模型
我开始使用 nhibernate 和 asp mvc2 开发一个 Web 应用程序(应用程序组合)。
我在正确更改应用程序的类别时遇到一些困难。
这是我的模型:
public class Application
{
public virtual int Application_ID{ get; private set; }
public virtual string Name { get; set; }
public virtual Category Category { get; set; }
}
public class Category : ILookupItem
{
public virtual int Category_ID { get; set; }
public virtual string Name { get; set; }
}
我的 viewModel:
public class ApplicationEditModel
{
public Application Application { get; set; }
public SelectList Categories { get; set; }
}
我的表单:
<% Html.BeginForm(new {id= Model.Application.Application_ID }); %>
<table>
<tr>
<td><%=Html.LabelFor(x => x.Application.Application_ID)%></td>
<td><%=Html.DisplayFor(x=>x.Application.Application_ID) %></td>
</tr>
<tr>
<td><%=Html.LabelFor(x=>x.Application.Name) %></td>
<td><%=Html.EditorFor(x=>x.Application.Name) %></td>
</tr>
<tr>
<td><%=Html.LabelFor(x=>x.Application.Category) %></td>
<td><%=Html.DropDownListFor(x=>x.Application.Category.Category_ID,Model.Categories,"Select a category") %></td>
</tr>
<tr><td><input type="submit" /></td></tr>
</table>
<% Html.EndForm(); %>
我的控制器操作:
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
Application app = _service.FindById(id);
TryUpdateModel<Application>(app, "Application");
_service.CommitChanges();
return RedirectToAction("Index");
}
我可以分配一个新类别,但如果我更改为不同的类别,我会收到以下消息:
Core.Model.Category 实例的标识符从 2 更改为 3
这似乎是因为 defaultmodelbinder 正在更新分配的类别的键,而不是使用新的新键分配新类别。
更新实体及其所有引用的正确方法是什么?
我也许可以使用自定义视图模型,将其绑定在控制器中,然后将其映射到我的域模型。但我担心它会给我太多的工作(最后我的应用程序模型中将有大约 100 个属性、30 个引用和 5-6 个列表)。
在这种情况下,Automapper 可以用来更新现有的域模型吗?
您如何处理此类更新?
I started to develop a web application (application portfolio) with nhibernate and asp mvc2.
I have some difficulties to properly change the category of an application.
Here are my models:
public class Application
{
public virtual int Application_ID{ get; private set; }
public virtual string Name { get; set; }
public virtual Category Category { get; set; }
}
public class Category : ILookupItem
{
public virtual int Category_ID { get; set; }
public virtual string Name { get; set; }
}
My viewModel:
public class ApplicationEditModel
{
public Application Application { get; set; }
public SelectList Categories { get; set; }
}
My Form:
<% Html.BeginForm(new {id= Model.Application.Application_ID }); %>
<table>
<tr>
<td><%=Html.LabelFor(x => x.Application.Application_ID)%></td>
<td><%=Html.DisplayFor(x=>x.Application.Application_ID) %></td>
</tr>
<tr>
<td><%=Html.LabelFor(x=>x.Application.Name) %></td>
<td><%=Html.EditorFor(x=>x.Application.Name) %></td>
</tr>
<tr>
<td><%=Html.LabelFor(x=>x.Application.Category) %></td>
<td><%=Html.DropDownListFor(x=>x.Application.Category.Category_ID,Model.Categories,"Select a category") %></td>
</tr>
<tr><td><input type="submit" /></td></tr>
</table>
<% Html.EndForm(); %>
My controller action:
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
Application app = _service.FindById(id);
TryUpdateModel<Application>(app, "Application");
_service.CommitChanges();
return RedirectToAction("Index");
}
I can assign a new category, but if I change to a different category I get the following message:
identifier of an instance of Core.Model.Category was altered from 2 to 3
This seems to be because the defaultmodelbinder is updating the key of the assigned category instead of assigning a new category with new new key.
What is the correct way to update an entity with all its references?
I could maybe use a custom view model, bind it in the controller, and then map it to my domain model. But I fear that it will give me too much work (at the end I will have around 100 properties,30 references and 5-6 lists in my application model).
Could Automapper be useful in this case to update an existing domain model?
How are you handling this kind of update?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您有一个非常简单的应用程序,否则很难使用相同的模型作为视图模型和域模型。使用单独的域模型的另一个原因是,为所有 100 个属性显示一个巨大的表单对于用户来说不太友好。您可以更好地向用户展示针对不同任务的不同形式。当您这样做时,无论如何,您最终都会为一个域实体获得不同的视图模型。
It is very hard to use the same model as both view and domain model unless you have a very simple application. Another reason to have a separate domain model is that it is not very user friendly to show a huge form for all of the 100 properties. You can better show the user different forms for different tasks. When you do that, you end up with different viewmodels for one domain entity anyway.