DropDownListFor ... selected value

In my model, I have a list of countries Model.ListCountry. The Country class has several fields: Id, Code, ValueFR, ValueUS

In my model, I have a Client, and this client has a country: Model.Customer.Country

I tried this:

@Html.DropDownListFor(x => x.Record.Customer.Country, new SelectList(Model.ListCountry, "Code", "FR"), new { id = "lbCountry" })

I do not know?

Thank,

Update1: I save the identifier in the database, but in the "parameter value" drop-down list I use the code, and since the display disables ValueFR or ValueUS depending on the user's language

+5
source share
1 answer

To pre-select a value from the drop-down list, set the appropriate property for this value in the action of your controller:

model.Record.Customer.Country = "FR";

, , SelectList, . , :

@Html.DropDownListFor(
    x => x.Record.Customer.Country, 
    new SelectList(Model.ListCountry, "Code", "ValueFR"), 
    new { id = "lbCountry" }
)

Code ValueFR . , model.Record.Customer.Country Code, , .

SelectList, 4 th:

@Html.DropDownListFor(
    x => x.Record.Customer.Country, 
    new SelectList(Model.ListCountry, "Code", "ValueFR", "FR"), 
    new { id = "lbCountry" }
)
+16

All Articles