mvc框架中如何实现查找表?

发布于 2024-10-14 06:57:10 字数 232 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如梦初醒的夏天 2024-10-21 06:57:10

假设您的表具有 IDNameValue 列。现在,如果只有一个表,该表很可能如下所示:

create table Lookup
(
    LookupID int not null identity
        primary key,
    LookupTypeID int not null
        references LookupType(LookupTypeID),
    Name nvarchar(50) not null,
    Value int not null,
    unique(EnumTypeID, Name)
)
go

该表将确保同一类型名称内不会发生冲突。

反正。您当然可以有一个类似的应用程序(而不是数据)模型类,

public class EnumValue
{
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public int Value { get; set; }
}

因此每当您获取某种类型的值时,您始终可以生成它们的 IList 并将它们提供给特定的下拉列表。

由于您没有提供任何有价值的数据,因此该表和类可以省略列/属性 Value,因为根据内部工作原理,可能根本不需要它。但您会最了解这一点,因为您了解您的应用程序要求。除了这个列/属性之外,只有 ID 可以做到这一点。

Suppose your tables have columns ID, Name and Value. Now by having only one table that table would most probably look like this:

create table Lookup
(
    LookupID int not null identity
        primary key,
    LookupTypeID int not null
        references LookupType(LookupTypeID),
    Name nvarchar(50) not null,
    Value int not null,
    unique(EnumTypeID, Name)
)
go

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

public class EnumValue
{
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public int Value { get; set; }
}

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.

━╋う一瞬間旳綻放 2024-10-21 06:57:10

我实际上使用了稍微不同的方法。我创建了一个具有复合主键、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!

旧梦荧光笔 2024-10-21 06:57:10

这是有关如何实现通用查找服务的教程。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文