Naved Ansari
Naved Ansari

Reputation: 660

how to populate a dropdownlist based on another dropdownlist selected value in mvc 4?

I have created two Drop Down List. On selecting a value from first dropdown list second dropdown list should be filled this is my code in View

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    @Html.DropDownList("Id", ViewData["Id"] as List<SelectListItem>, new { })
    <button type="submit">next</button>
} 
@Html.DropDownList("Id", ViewData["Id1"] as List<SelectListItem>)

When I click on next button second dropdown is getting populated. Same thing I want to do when I select a particular item from first list. I don't want to use jquery is there any way to do? Or is there any way how I can generate postback when I select value from first list?

Upvotes: 0

Views: 1480

Answers (1)

JoaoRibeiro
JoaoRibeiro

Reputation: 928

I think you can put the first dropdown inside a form and include the change attribute to this dropdown:

@{ Html.BeginForm("Action", "Controller", FormMethod.Post, new { enctype = "multipart/form-data" }); }

    @Html.DropDownList("Id", ViewData["Id"] as List<SelectListItem>, new { onchange = "this.form.submit();" })

@{ Html.EndForm(); }

Then, in the controller, you should include some code to populate the second dropdown.

Upvotes: 1

Related Questions