如何静态填写MVC3下拉列表 [英] How to fill dropdown list statically in MVC3

查看:93
本文介绍了如何静态填写MVC3下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何填充静态下拉列表(硬codeD)

How to fill dropdown list statically(Hardcoded)

我有2个项目

文本=活动值=A

文本=无效值=I

和保存到数据库上面的值

and above values saved into database

谢谢,

推荐答案

如果你想要做的这一切都在一个视图中,code是这样的:

If you want to do this all in a view, the code would look like this:

@{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
        dictionary.Add("Active", "A");
        dictionary.Add("InActive", "I");
        SelectList list = new SelectList(dictionary, "value", "key");
}
@Html.DropDownList("name", list)

此外,如果你想添加一个空白,它是这样的:

Also if you wanted to add a blank, it would look like this:

@{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
        dictionary.Add("", "");
        dictionary.Add("Active", "A");
        dictionary.Add("InActive", "I");
        SelectList list = new SelectList(dictionary, "value", "key");
}
@Html.DropDownList("name", list)

如果你想有一个特定的项目选择,它是这样的:

And if you wanted to have a particular item selected, it would look like this:

@{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
        dictionary.Add("", "");
        dictionary.Add("Active", "A");
        dictionary.Add("InActive", "I");
        SelectList list = new SelectList(dictionary, "value", "key", "I");
}
@Html.DropDownList("name", list)

请注意,我改变了添加参数的SelectList 线,这相当于InActice的价值使其在页面加载时进行选择。

Notice I changed the SelectList line adding a "I" parameter, this corresponds to the 'InActice' value causing it to be selected on page load.

这code将在HTML生成这样的:

This code would generate this in HTML:

<select id="name" name="name">
    <option value="A">Active</option>
    <option selected="selected" value="I">InActive</option>
</select>

因此​​,如果这将是静态的,你可以只用这个。您可以删除选择=选择如果需要的话。

我不完全相信你的保存到数据库上面的值的意思,你有一个模型去与这些项目吗?如果您编辑您的问题,并添加你的努力做的,我可以帮你更好的一些例子。

I'm not exactly sure what you mean by "above values saved into database", do you have a model to go with these items? If you edit your question and add some examples of what your trying to do I can help you better.

这篇关于如何静态填写MVC3下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆