Nowadays, a very common task for any MVC developer is "How to handle Dropdown list Selected Index Changed event in MVC?". This article will show how to handle it in different ways.
- View with Form Post method
- Partial View with ajax/jQuery GET method
First of all, we will take an example where we need to show Employee data on the page, and one DropDownList for Employee List. And on selection change of DropDownList will show employee records based on selected Employee Id.
So first, we will make a model for Employee.
EmployeeModel:
- public class EmployeeModel
- {
- public int EmployeeId
- {
- get;
- set;
- }
- public string EmpName
- {
- get;
- set;
- }
- public double Salary
- {
- get;
- set;
- }
- public List < EmployeeModel > listEmp
- {
- get;
- set;
- }
- //This property will be used to populate employee dropdownlist
- public IEnumerable < SelectListItem > EmployeeListItems
- {
- get
- {
- return new SelectList(listEmp, "EmployeeId", "EmpName");
- }
- }
- }
Now, we need to create a method to get data from the database (Here in this article, I am not showing how to get data from the database, just creating data in method only). You can change the functionality of this method to write SQL query or any other way to fetch data from your database server.
- #region private method to get employee records form database
- private List < EmployeeModel > GetEmployeeDataFromDB()
- {
- //Here you can write your query to fetch data from db
- var listEmp = new List < EmployeeModel > ();
- var emp1 = new EmployeeModel();
- emp1.EmployeeId = 1;
- emp1.EmpName = "Employee1";
- emp1.Salary = 50000;
- var emp2 = new EmployeeModel();
- emp2.EmployeeId = 2;
- emp2.EmpName = "Employee2";
- emp2.Salary = 56000;
- var emp3 = new EmployeeModel();
- emp3.EmployeeId = 3;
- emp3.EmpName = "Employee3";
- emp3.Salary = 60000;
- listEmp.Add(emp1);
- listEmp.Add(emp2);
- listEmp.Add(emp3);
- return listEmp;
- }
- #endregion
Now, we will create an Employee controller that will call above method to get employee records and return it to the corresponding View through Employee model.
- public class EmployeeController: Controller
- {
- #region Example with Form Post method
- public ActionResult Employee()
- {
- var employeeModel = new EmployeeModel();
- employeeModel.listEmp = GetEmployeeDataFromDB();
- //Set default employee records
- employeeModel.EmployeeId = employeeModel.listEmp.First().EmployeeId;
- employeeModel.EmpName = employeeModel.listEmp.First().EmpName;
- employeeModel.Salary = employeeModel.listEmp.First().Salary;
- return View(employeeModel);
- }
- [HttpPost]
- public ActionResult Employee(EmployeeModel model)
- {
- var employeeModel = new EmployeeModel();
- employeeModel.listEmp = GetEmployeeDataFromDB();
- //Filter employeeData based on EmployeeId
- //This filter part you can do through sql queries also.
- //Here model.EmployeeId is Dropdownlist selected EmployeeId.
- var emp = employeeModel.listEmp.Where(e => e.EmployeeId == model.EmployeeId).FirstOrDefault();
- //Set default emp records
- employeeModel.EmployeeId = emp.EmployeeId;
- employeeModel.EmpName = emp.EmpName;
- employeeModel.Salary = emp.Salary;
- return View(employeeModel);
- }
- #endregion
- }
As dropdown list selection will be changed, Form Post method will be called and that is second Action Employee() mentioned in above sample.
We will see later how dropdown list selected index changed call Post Action.
View
1. View with Form Post method
In this section, we will see how View works with Form Post method.
We need one view to Employee Records.
- @model MvcApplication1.Models.EmployeeModel
- @{
- ViewBag.Title = "Employee";
- }
- <h2>Employee</h2>
- @using (Html.BeginForm("Employee", "Employee", FormMethod.Post, new { id = "demoForm", name = "demoForm" }))
- {
- @Html.DropDownListFor(m => m.EmployeeId, Model.EmployeeListItems, "---Select Employee---", new { Class = "ddlStyle", onchange = "SelectedIndexChanged()" })
- <fieldset>
- <legend>EmployeeModel</legend>
- <div class="display-label">
- <strong> @Html.DisplayNameFor(model => model.EmployeeId) </strong>
- </div>
- <div class="display-field">
- @Html.DisplayFor(model => model.EmployeeId)
- </div>
- <div class="display-label">
- <strong> @Html.DisplayNameFor(model => model.EmpName) </strong>
- </div>
- <div class="display-field">
- @Html.DisplayFor(model => model.EmpName)
- </div>
- <div class="display-label">
- <strong> @Html.DisplayNameFor(model => model.Salary) </strong>
- </div>
- <div class="display-field">
- @Html.DisplayFor(model => model.Salary)
- </div>
- </fieldset>
- }
- <script type="text/javascript">
- function SelectedIndexChanged() {
- //Form post
- document.demoForm.submit();
- }
- </script>
DropDownList Selected Index Changed Event: In above View sample, the dropdown list has an on change event and that will hit JavaScript method SelectedIndexChanged().
- <script type="text/javascript">
- function SelectedIndexChanged()
- {
- //Form post
- document.demoForm.submit();
- }
- </script>
How Selected Employee Id will be passed to controller action
If you have noticed we have mentioned Model.EmployeeId as an expression.
- // An expression that identifies the object that contains the properties to
- // display.
- @Html.DropDownListFor(m => m.EmployeeId, Model.EmployeeListItems............
Post action Employee(Employee model)
This action will fetch employee records through a private method GetEmployeeDataFromDB() in above controller sample.
It will filter data based on Model.EmployeeId and return filtered model again to view.
2. Partial View with ajax/jQuery GET method
This section will describe how we can use Partial View to load Employee Records based on Selected Employee Id in dropdown list.
For this now we will have two separate Actions so it will not be confused with the above sample.
Actions that will support Partial View:
- #region Example with Partial View
- // GET: /Employee/
- public ActionResult Index()
- {
- var employeeModel = new EmployeeModel();
- employeeModel.listEmp = GetEmployeeDataFromDB();
- //Set default emp records
- employeeModel.EmployeeId = employeeModel.listEmp.First().EmployeeId;
- employeeModel.EmpName = employeeModel.listEmp.First().EmpName;
- employeeModel.Salary = employeeModel.listEmp.First().Salary;
- return View(employeeModel);
- }
- /// <summary>
- /// This method will return PartialView with Employee Model
- /// </summary>
- /// <param name="EmployeeId"></param>
- /// <returns></returns>
- public PartialViewResult GetEmployeeRecord(int EmployeeId)
- {
- var employeeModel = new EmployeeModel();
- employeeModel.listEmp = GetEmployeeDataFromDB();
- var emp = employeeModel.listEmp.Where(e => e.EmployeeId == EmployeeId).FirstOrDefault();
- //Set default emp records
- employeeModel.EmployeeId = emp.EmployeeId;
- employeeModel.EmpName = emp.EmpName;
- employeeModel.Salary = emp.Salary;
- return PartialView("_EmpTestPartial", employeeModel);
- }#endregion
And second Action GetEmployeeRecord (int EmployeeId) is to return a partial view "_EmpTestPartial" with filtered employee records based on passed EmployeeId parameter.
Partial View
Create a partial view to show Employee Records.
- @model MvcApplication1.Models.EmployeeModel
- <fieldset>
- <legend>EmployeeModel</legend>
- <div class="display-label">
- <strong> @Html.DisplayNameFor(model => model.EmployeeId) </strong>
- </div>
- <div class="display-field">
- @Html.DisplayFor(model => model.EmployeeId)
- </div>
- <div class="display-label">
- <strong> @Html.DisplayNameFor(model => model.EmpName) </strong>
- </div>
- <div class="display-field">
- @Html.DisplayFor(model => model.EmpName)
- </div>
- <div class="display-label">
- <strong> @Html.DisplayNameFor(model => model.Salary) </strong>
- </div>
- <div class="display-field">
- @Html.DisplayFor(model => model.Salary)
- </div>
- </fieldset>
Index View
- @model MvcApplication1.Models.EmployeeModel
- @
- {
- ViewBag.Title = "Index";
- } < h2 > EmployeeResult < /h2>
- @Html.DropDownListFor(m => m.EmployeeId, Model.EmployeeListItems, "---Select Employee---", new
- {
- Class = "ddlStyle", id = "ddlEmployee"
- }) < div id = "partialDiv" >
- @Html.Partial("_EmpTestPartial") < /div> < script >
- $(document).ready(function()
- {
- $("#ddlEmployee").on("change", function()
- {
- $.ajax(
- {
- url: '/Home/GetEmployeeRecord?EmployeeId=' + $(this).attr("value"),
- type: 'GET',
- data: "",
- contentType: 'application/json; charset=utf-8',
- success: function(data)
- {
- $("#partialDiv").html(data);
- },
- error: function()
- {
- alert("error");
- }
- });
- });
- }); < /script>
- <div id="partialDiv">
- @Html.Partial("_EmpTestPartial")
- </div>
- @Html.DropDownListFor(m => m.EmployeeId, Model.EmployeeListItems, "---Select Employee---", new { Class = "ddlStyle", id = "ddlEmployee" })
jQuery event for DropDownList Selected Index Changed event
DropDownList control has ID ddlEmployee and onchange method for this control is mentioned in jQuery document.ready() function.
- $(document).ready(function()
- {
- $("#ddlEmployee").on("change", function()
- {
- $.ajax(
- {
- url: '/Employee/GetEmployeeRecord?EmployeeId=' + $(this).attr("value"),
- type: 'GET',
- data: "",
- contentType: 'application/json; charset=utf-8',
- success: function(data)
- {
- $("#partialDiv").html(data);
- },
- error: function()
- {
- alert("error");
- }
- });
- });
- }); < /script>
$.ajax({.....}) is a method to call a given Url without loading full page that's why we should use Partial View to refresh data on a page.
Selected EmployeeId in DropdownList is passed in GetEmployeeRecord() action through Querystring.
url: '/Home/GetEmployeeRecord?EmployeeId=' + $(this).attr("value")
How returned Partial View will be loaded on View
In Ajax GET method, we have written success method. If called Action GetEmployeeRecord(int EmployeeId) will return Partial View successfully so returned partial view will be placed in div "partialDiv", as below it shown.
- success: function (data) {
- $("#partialDiv").html(data);
- },

I hope this article will help to developers who are/will be looking for DropdownList selected index changed event in MVC.
I have attached source code also. You can download full code as explained above in this blog.

carlos montanaPosted Sep 23, 2019, 11:05 PM
Im getting this error= 'Value cannot be null.' in the models seleclistitem when im doing the selectedchange, im using aspnetcore what i can do to resolve this problema?
Cheryl JacksonPosted Jan 24, 2019, 3:33 PM
Thank you for this article - I got the View with Form Post working, but the partial is not - the script for $(this).attr("value") is not returning my value, although I can see it in the debugger under this.value
Hitanshi MehtaPosted Oct 13, 2018, 3:08 AM
Thank you so much for article.
Kandarp RamiPosted Apr 29, 2018, 11:06 AM
Thank you so much, Mr.Tripathi. your code so helped me. once again thank you.
sonali borikarPosted Feb 16, 2018, 2:45 AM
Var TwoDList = (from ctx in db.TwoDDiagnostics select new { ctx.TwoDDiagnostic_Name}).ToList().Distinct(); string[] twod = new string[TwoDList.Count()]; //int count = 0; foreach (var item in TwoDList) { twod[count] = item.TwoDDiagnostic_Name; count++; } count = 0; var fromDatabaseEF = new SelectList(twod.ToList()); ViewData["DBMySkills"] = fromDatabaseEF;
sonali borikarPosted Feb 16, 2018, 2:44 AM
<td> @Html.DropDownList("Investigation_name", (IEnumerable<SelectListItem>)ViewData["DBMySkills1"], "-Select-", htmlAttributes: new { @class = "form-control", @style = "width:50%" })</td>
sonali borikarPosted Feb 16, 2018, 2:43 AM
<div class="form-inline"> <label for="inputEmail" class=" col-lg-2 col-sm-2 control-label">2D Diagnostic</label> <div class="col-lg-5"> <td> @Html.DropDownList("Investigation_name", (IEnumerable<SelectListItem>)ViewData["DBMySkills"], "-Select-", htmlAttributes: new { @class = "form-control" , @style= "width:50%"}) </td> </div> <div class="col-lg-3"> @Html.TextBoxFor(model => model.Investigation_Price, htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.Investigation_Price) </div> </div>
sonali borikarPosted Feb 16, 2018, 2:43 AM
Vd Tripathi i have fetched only name from database which is given below i had mentioned now i want to get price of name after selecting option from dropdown in textbox.
Osama HassanPosted Mar 24, 2017, 8:31 AM
In which file have to put method to get the database record into DDL?
Karthik ElumalaiPosted Feb 17, 2017, 4:40 AM
Good one.thanks for sharing
Gurvinder SinghPosted Dec 20, 2016, 7:05 AM
Hi VD, Thanks for this, but i am getting error in partial view. Pls help
Monibrata BhattacharjeePosted Mar 31, 2016, 6:03 AM
Its really a good and well explained article.
Vishwakant TripathiPosted Mar 30, 2016, 4:04 AM
Thanks to everyone.
Jaipal ReddyPosted Mar 30, 2016, 12:25 AM
Nice. .
Ankur MistryPosted Mar 29, 2016, 11:19 PM
Nice share
Debasis SahaPosted Mar 29, 2016, 10:50 AM
good
Saillesh PawarPosted Mar 29, 2016, 10:49 AM
nice share
Imtiyaz AnsariPosted Mar 29, 2016, 9:58 AM
Nice Article
sandipPosted Mar 29, 2016, 8:44 AM
Good effort Tripathi Jee
lourdu reddyPosted Mar 29, 2016, 8:20 AM
Great job Tripathi. Great explanation.