There are a couple of ways of setting the default value of a dropdown list. If you are using ViewBag and Razor this is one way to control the default value. This is helpful in the Create page.
ViewBag
The ViewBag provides a simple way to specify the default value when you are already using a SelectList. Just enter the default value as one of the parameters. For example, imagine if you have a list of Cities and you are setting the CityId to a list of cities in your controller:
ViewBag.CityId = new SelectList(db.Citiess, "CityId", "Title",1);
In your view your HTML would look like:
@Html.DropDownList("DestinationId", string.Empty)
NOTE: This gives you the ability to control the default value without specifying it in the View itself.
Here is another way to do the same thing:
@Html.DropDownList("ReviewItemtypeId",(SelectList)ViewBag.ReviewItemTypeId)
Other Ways
Check out this article: http://stackoverflow.com/questions/6807256/dropdownlist-set-selected-value-in-mvc3-razor
Share This To :
0 comments:
Post a Comment