多对多的ASP.NET MVC绑定

时间:2022-09-18 08:41:49

If you have scenario of a User that has many Roles and Roles that have many users. Using MVC is there an easy way to bind to the model directly? If your user class has a list of roles and the roles class has a list of users.

如果您的用户具有许多具有许多用户的角色和角色的场景。使用MVC有一种直接绑定到模型的简单方法吗?如果您的用户类具有角色列表,并且角色类具有用户列表。

Thank you.

谢谢。

1 个解决方案

#1


1  

Model binding in MVC is fairly strong. You have to remember that naming conventions are important, especially when you start attempting to bind collections. To bind to a collection, you have to name your form field something to the effect of name="Model.Roles[]" (where Model is User; that would be dependent on your view being strongly typed to your User object; change accordingly). The brackets denote a collection of information being sent back up to the server. If you want to target a specific point in the collection, perhaps targeting a specific property on that point, you might have something like name="Model.Roles[x].Id where x is a valid index in the collection. It takes some effort to ensure you have these correct, and my approach almost always degrades into sending a custom collection of information back to the server and manually creating the relationships in the action method prior to saving the entity in question.

MVC中的模型绑定相当强大。您必须记住命名约定很重要,尤其是当您开始尝试绑定集合时。要绑定到集合,您必须将表单字段命名为name =“Model.Roles []”的效果(其中Model是User;这取决于您的视图被强类型化为您的User对象;相应地更改)。括号表示一组信息被发送回服务器。如果你想要定位集合中的特定点,也许是针对该点上的特定属性,你可能会有类似name =“Model.Roles [x] .Id的东西,其中x是集合中的有效索引。它需要一些努力确保您拥有这些正确的,我的方法几乎总是降级为将自定义信息集合发送回服务器并在保存相关实体之前手动创建操作方法中的关系。

Phil Haack has a post about binding to a collection, albeit from 2008. It's still relevant, however, and might give you more insight into the model binding behavior.

Phil Haack有一篇关于绑定到一个集合的帖子,虽然从2008年开始。但它仍然相关,可能会让你更深入地了解模型绑定行为。

#1


1  

Model binding in MVC is fairly strong. You have to remember that naming conventions are important, especially when you start attempting to bind collections. To bind to a collection, you have to name your form field something to the effect of name="Model.Roles[]" (where Model is User; that would be dependent on your view being strongly typed to your User object; change accordingly). The brackets denote a collection of information being sent back up to the server. If you want to target a specific point in the collection, perhaps targeting a specific property on that point, you might have something like name="Model.Roles[x].Id where x is a valid index in the collection. It takes some effort to ensure you have these correct, and my approach almost always degrades into sending a custom collection of information back to the server and manually creating the relationships in the action method prior to saving the entity in question.

MVC中的模型绑定相当强大。您必须记住命名约定很重要,尤其是当您开始尝试绑定集合时。要绑定到集合,您必须将表单字段命名为name =“Model.Roles []”的效果(其中Model是User;这取决于您的视图被强类型化为您的User对象;相应地更改)。括号表示一组信息被发送回服务器。如果你想要定位集合中的特定点,也许是针对该点上的特定属性,你可能会有类似name =“Model.Roles [x] .Id的东西,其中x是集合中的有效索引。它需要一些努力确保您拥有这些正确的,我的方法几乎总是降级为将自定义信息集合发送回服务器并在保存相关实体之前手动创建操作方法中的关系。

Phil Haack has a post about binding to a collection, albeit from 2008. It's still relevant, however, and might give you more insight into the model binding behavior.

Phil Haack有一篇关于绑定到一个集合的帖子,虽然从2008年开始。但它仍然相关,可能会让你更深入地了解模型绑定行为。