Home MVC @Html.DropDownListFor change displayed value in javascript
Reply: 0

MVC @Html.DropDownListFor change displayed value in javascript

user1297 Published in July 16, 2018, 6:38 am

Using MVC 4, I am trying to change the displayed value in the view for @Html.DropDownListFor, based on the action of another drop down list. In the below example, when the user selects any value in the state list, I want the @Html.DropDownListFor to display the indexed 203 value which is US. How to do this?

//in the controller

ViewBag.statelist = new SelectList(db.states, "state_abbr", "state_name");

ViewBag.countrylist = new SelectList(db.countries, "country_id", "country_name", -1);

//in the View

<div class="form-group">
    @Html.LabelFor(model => model.states)
    @Html.DropDownList("ddlstate", ViewBag.statelist as SelectList, Model.rrq_sec1_waddr_stprov, htmlAttributes: new { @onchange="StateChange()" })

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

function StateChange() {

    var state = document.getElementById("ddlstate").value;
    $("#ddlcountry").val = 203;

How do I get the #ddlcountry index=203 which is "US" show up in view then the state drop down changes?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.328944 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO