帮助与asp.net的MVC选择下拉列表 [英] Help with asp.net mvc select dropdown
本文介绍了帮助与asp.net的MVC选择下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的用户表中的婚姻状况字段,它只是VARCHAR
但我只是想给用户的四个选项(已婚,单身,丧偶和离婚),我想有一个正确时,即时通讯编辑表单中选择..这可能吗?请帮助。
I have a marital status field in my users table that its just varchar yet I only want to give the users four options (married, single, widowed and divorced) and i want to have the correct one selected when Im editing the form.. is it possible? please help.
推荐答案
这应该指向你在正确的方向:
This should point you in the right direction:
<%= Html.DropDownList("listName", new string[] { "Married", "Single", "Widowed", "Divorced" }
.Select(m => new SelectListItem(){
Selected = model.MaritalStatus == m,
Text = m,
Value = m
})); %>
假设你的模型有一个'MaritalStatus字段,
Assuming that your model has a 'MaritalStatus' field,
Selected = model.MaritalStatus == m
将默认选择模型的状态。
will select the status of your model by default.
这篇关于帮助与asp.net的MVC选择下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文