开发者

Help with asp.net mvc select dropdown

开发者 https://www.devze.com 2022-12-18 19:25 出处:网络
I have a marital status field in m开发者_JAVA百科y 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 corre

I have a marital status field in m开发者_JAVA百科y 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
})); %>

Assuming that your model has a 'MaritalStatus' field,

Selected = model.MaritalStatus == m

will select the status of your model by default.


This blog post should lead you in the right direction:

http://weblogs.asp.net/ashicmahtab/archive/2009/03/27/asp-net-mvc-html-dropdownlist-and-selected-value.aspx

you'd have to give us a little more information and maybe a code sample of what you have so far to get a more specific answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号