asp.net mvc 自定义 modelbinder - 如何使用关联实体执行更新
我试图了解使用自定义模型绑定程序时如何更新关联实体。
如果您有一个与类别实体有关系的产品实体,并且您在表单的下拉列表中显示产品的类别选择列表。
用户分配一个新类别,并且该更改需要与产品一起保留。如何实现绑定来分配更新的类别? Product 的属性很简单,但是如何设置 Product.Category = 类别呢?
希望这是清楚的:-)
I am trying to understand how associated entities are updated when using custom modelbinders.
If you have a Product entity with a relationship to a Category entity and you are displaying a list of category choice for a product in a dropdown on a form.
The the user assigns a new category and that change needs to be persisted with the Product. How is the binding implemented to assign the updated category? The properties f the Product are easy enough, but how do you set the Product.Category = category?
Hope that is clear :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您的自定义模型绑定就在那里,您只是想建立产品和类别之间的关系。
为此,您需要执行以下操作:
product.CategoryReference.EntityKey =
new EntityKey("上下文.类别", "ID", 类别ID);
这将简单地更新实体中的外键关系。
Sounds like your custom model binding is there, you're just trying to setup up your relationship between your Product and Category.
To do this you would do something like this:
product.CategoryReference.EntityKey =
new EntityKey("Context.Category", "ID", categoryID);
That will simply update the foreign key relationship in your entity.