@Html.DropDownList("Category", new List<SelectListItem>
{
new SelectListItem{ Text="Residentials", Value = "Residentials" },
new SelectListItem{ Text="Leasings", Value = "Residentials" },
new SelectListItem{ Text="Rentals", Value = "Residentials" },
new SelectListItem{ Text="Commercials", Value = "Residentials" }
})
i am useing this dropdownlist to create a record.
but when in edit mode.
it always show Residentials as selected item
i want it to have it selected item form the model.
Shanker Paudel
Junior Web Developer
Chandigarh,India
If you want an initial selected value from your model do this:
@Html.DropDownList("Category", new List<SelectListItem>
{
new SelectListItem{ Text="Leasings", Value = "Residentials" },
new SelectListItem{ Text="Rentals", Value = "Residentials" },
new SelectListItem{ Text="Commercials", Value = "Residentials" }
}, model.ValueYouWantSelectedFirst)
You can also load values from Model directly into the dropdownlist like so:
new SelectListItem{ Text=Model.YourValueToDisplay, Value = "Residentials" },
dev.paudel
Member
218 Points
189 Posts
Dropdownlist in MVC
Feb 27, 2013 01:35 PM|LINK
@Html.DropDownList("Category", new List<SelectListItem>
{
new SelectListItem{ Text="Residentials", Value = "Residentials" },
new SelectListItem{ Text="Leasings", Value = "Residentials" },
new SelectListItem{ Text="Rentals", Value = "Residentials" },
new SelectListItem{ Text="Commercials", Value = "Residentials" }
})
i am useing this dropdownlist to create a record.
but when in edit mode.
it always show Residentials as selected item
i want it to have it selected item form the model.
Junior Web Developer
Chandigarh,India
jp.lima
Member
186 Points
51 Posts
Re: Dropdownlist in MVC
Feb 27, 2013 01:52 PM|LINK
If you want an initial selected value from your model do this:
@Html.DropDownList("Category", new List<SelectListItem> { new SelectListItem{ Text="Leasings", Value = "Residentials" }, new SelectListItem{ Text="Rentals", Value = "Residentials" }, new SelectListItem{ Text="Commercials", Value = "Residentials" } }, model.ValueYouWantSelectedFirst)You can also load values from Model directly into the dropdownlist like so:
new SelectListItem{ Text=Model.YourValueToDisplay, Value = "Residentials" },Xequence
Contributor
4313 Points
1528 Posts
Re: Dropdownlist in MVC
Feb 27, 2013 04:16 PM|LINK
@html.dropdownlistfor. build your object with a property selectedId and set your selectedid in your constructor of your model object.
http://stackoverflow.com/questions/10210305/get-selected-value-from-html-dropdownlist-back-to-controller-net-mvc
Credentials
rajendraram
Participant
812 Points
258 Posts
Re: Dropdownlist in MVC
Feb 27, 2013 04:20 PM|LINK
Please see this sample
http://www.highoncoding.com/Articles/770_Implementing_Dynamic_DropDownList_in_ASP_NET_MVC_3_Framework.aspx
http://forums.asp.net/t/1467006.aspx/1