mvc框架中如何实现查找表?
Asp.net MVC2:我的 mvc 应用程序中有很多下拉列表。首先,我首先为每个表创建一个具有唯一 ID 和名称的表,并在控制器和视图中引用它们。应用程序变得更大,建议我使用查找表,其中包含查找类型和查找值作为复合主键,并填充其中下拉列表的所有值。我查遍了整个互联网,用于 mvc 的唯一方法是每个下拉列表一个表!有人可以向我解释如何实现它,请详细说明,因为我完全迷失了。教程的链接也很棒。 我正在使用 vb.net 和 linq to sql。
Asp.net MVC2: I have many dropdownlists in my mvc application. At first, I started by creating a table for each one with a unique ID and name and referring to them in the controllers and views. The application got bigger, and it was suggested that I use a lookup table, that contains lookuptype and lookupvalue as compound primary key and fill all the values for the dropdownlists in it. I've looked all over the internet, the only method used for mvc is one table for each dropdownlist! Can someone explain to me how I can implement it, and in detail please becoz I'm totally lost. A link to a tutorial would also be great.
I'm using vb.net and linq to sql.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您的表具有
ID
、Name
和Value
列。现在,如果只有一个表,该表很可能如下所示:该表将确保同一类型名称内不会发生冲突。
反正。您当然可以有一个类似的应用程序(而不是数据)模型类,
因此每当您获取某种类型的值时,您始终可以生成它们的
IList
并将它们提供给特定的下拉列表。由于您没有提供任何有价值的数据,因此该表和类可以省略列/属性
Value
,因为根据内部工作原理,可能根本不需要它。但您会最了解这一点,因为您了解您的应用程序要求。除了这个列/属性之外,只有 ID 可以做到这一点。Suppose your tables have columns
ID
,Name
andValue
. Now by having only one table that table would most probably look like this:This table will make sure that within the same type names don't clash.
Anyway. You could of course have a similar application (not data) model class
So whenever you get values of a certain type you can always generate an
IList<EnumValue>
of them and feed them to a particular drop down.Since you didn't provide any valuable data this table and class could omit column/property
Value
, because depending on the inner workings it may not be needed at all. But you will know this best because you know your application requirements. Instead of this column/property only ID could do the trick.我实际上使用了稍微不同的方法。我创建了一个具有复合主键、LookupName 和 LookupValue 的表。然后在数据上下文中,我声明了一个方法,该方法将查找名称作为参数,然后带来查找名称与该参数匹配的查找值的列表。在原始表(例如联系人)中,我创建了一个名为“状态”的字段,其中将保存所选值。然后在控制器中,我使用 viewdata 创建下拉列表。
示例: _db 代表数据上下文
Viewdata('Status')= new selectlist(_db.Getlookupname('status'),'lookupname','lookupname')
然后在视图
html.dropdownlist('status')
中我也命名了下拉列表具有与目标表“联系人”中找到的相同字段名“状态”。
它有效,没有任何复杂性或错误。
感谢您的帮助。我希望这对其他人有帮助!
I actually used a slightly different approach. I created a table that has a composite primary key, LookupName and LookupValue. Then in the datacontext I declared a method that takes the lookupname as a parameter, and then brings a list of lookupvalues whose lookupname match this parameter. In the original table (ex.Contact) I created a field called status, where the selected value will be saved. Then in the controller, I used viewdata to create dropdownlists.
Example: _db represents the datacontext
Viewdata('Status')= new selectlist(_db.Getlookupname('status'),'lookupname','lookupname')
and then in the view
html.dropdownlist('status')
I also named the dropdownlist with the same fieldname 'status' that is found in the target table 'Contact'.
And it worked, without any complexity or errors.
Thanks for the help. And I hope this will be helpful to someone else!
这是有关如何实现通用查找服务的教程。
http://wtfpermin.blogspot.com/2011/02 /working-with-reference-data-lookups.html
与您所做的类似,但可能更全面一些。
Here's a sort of tutorial on how to achieve a generic lookup service.
http://wtfperminute.blogspot.com/2011/02/working-with-reference-data-lookups.html
Similar to what you've done, but perhaps a bit more comprehensive.