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

MVC @Html.DropDownListFor change displayed value in javascript

e2fx Published in 2017-12-07 21:54:15Z

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?

Aamir Ali
Aamir Ali Reply to 2017-12-08 05:12:37Z

This might help u to select value in second dropdown, onchange event of first dropdown using this jQuery function.

 $('#ddlstate').change(function () {
 $("#ddlcountry").val( $('#ddlstate').val());
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO