MVC 4 - 下拉列表选择的值 [英] MVC 4 - dropdownlist selected value

查看:75
本文介绍了MVC 4 - 下拉列表选择的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以向我解释如何为MVC 4中的下拉列表设置selctedvalue。以下是我的下拉列表。



Anyone could explain me how to set selctedvalue for a dropdownlist in MVC 4. Below is my dropdownlist.

@Html.DropDownListFor(model => model.Role,
               new List<SelectListItem>
               {
                   new SelectListItem { Text = "Engineer", Value = "1"},
                   new SelectListItem { Text = "Librarian", Value = "2"},
                   new SelectListItem { Text = "Admin", Value = "3"}
               }
              )





这是我的编辑页面,我需要在页面打开时默认选定的角色。

先谢谢。



Vijay



This is my edit page, i need to default the selected role when page got opened.
Thanks in advance.

Vijay

推荐答案

使用 SelectList 绑定 @HtmlDropdownListFor 并在其中指定 selectedValue 参数。 />


http:/ /msdn.microsoft.com/en-us/library/dd492553(v=vs.108).aspx [ ^ ]





Use SelectList to bind @HtmlDropdownListFor and specify selectedValue parameter in it.

http://msdn.microsoft.com/en-us/library/dd492553(v=vs.108).aspx[^]


public SelectList(
    IEnumerable items,
    string dataValueField,
    string dataTextField,
    Object selectedValue
)


@ Html.DropDownListFor(m => m.Education,

new List; SelectListItem>

{

new SelectListItem {Text =B.Sc,Value =B.Sc},

new SelectListItem {Text =MBBS,Value =MBBS },

新的SelectListItem {Text =BA,Value =BA}

}



您可以使用此代码但如果您有此DropDownList的数据库
@Html.DropDownListFor(m => m.Education,
new List;SelectListItem>
{
new SelectListItem { Text = "B.Sc", Value = "B.Sc"},
new SelectListItem { Text = "M.B.B.S", Value = "M.B.B.S"},
new SelectListItem { Text = "B.A", Value = "B.A"}
}
)
You Can Use this code But If You Have Data Base For This DropDownList


这篇关于MVC 4 - 下拉列表选择的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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