Search code examples
asp.net-mvcdefaulthtml.dropdownlistfor

how to make default value for @HTMLdropdownlistfor in MVC


Iam passing a viewbag to view as a selectlist as below

    ViewBag.Currency = new SelectList(db.portal_lookup.Where(c => c.lookup_type 
== "FND_CURRENCY").OrderBy(m => m.lookup_description), "lookup_id", "lookup_description");

in view i show it as below

     @Html.DropDownListFor(model => model.currency_code,
(SelectList)@ViewBag.Currency,"",new {  @class = "m-wrap" })

by default the value of this dropdownlistfor should be "IND" how can i set that?


Solution

  • Your best bet is probably to fill up the model that you pass to the view, assuming that you're passing one, whereby you set

    model.currency_code = "IND".
    

    Alternatively, you could use the overload for new SelectList that allows you to specify the selected value:

    SelectList Constructor (IEnumerable, String, String, Object) Initializes a new instance of the SelectList class by using the specified items for the list, the data value field, the data text field, and a selected value.

    ViewBag.Currency = new SelectList(db.portal_lookup.Where(c => c.lookup_type == "FND_CURRENCY").OrderBy(m => m.lookup_description), "lookup_id", "lookup_description", "IND");
    

    Hope that helps.