2017-12-08 1 views
0

In meiner MVC Ansicht habe ich eine Länderliste, die mit allen Ländern bevölkert ist. Dies funktioniert in Ordnung und der Benutzer wählt verschiedene Länder aus. & Sie werden in Ordnung angezeigt. Ich möchte die Fähigkeit in Javascript, dieses Feld zurück zu "Select Country" zu ändern, wie mache ich das?mvc razor dropdownlistfor display standard von javascript

Dies ist meiner Ansicht nach

<div class="form-group"> 
    @Html.DropDownListFor(model => model.country, ViewBag.countrylist as SelectList, "Select Country", htmlAttributes: new { @id = "ddlcountry" }) 
</div> 

Dies ist mein Javascript

$("#ddlcountry").val(44); //this works to set the country code to 44 
$("#ddlcountry").val("Select Country"); //this does not work to display "Select Country" like the default when you first load the page 

Wie kann ich die Standardeinstellung "Land auswählen" Show haben wieder?

Antwort

0

Ihre DropDownListFor() Methode ist die Erzeugung der 'Label Option' mit einem null Wert

<option value="">Select Country</option> 

diese Option mit jQuery auszuwählen verwendet

$("#ddlcountry").val(''); 
+0

perfekt gearbeitet - danke! – e2fx