This article will show you how you can make dropdownlist
autopostback functionality in asp.net mvc using c#.net. This article you can use MVC3/MVC4/MVC5/MVC6.
Some of my previous articles are as follows: XML
File Generation And Data Writing Using C#.Net In Asp.Net MVC, How
To Read XML File in DataTable Using C#.Net In Asp.Net MVC, Cookies
Create, Read and Delete Operation Using jQuery in Asp.Net, Bind
XML File Data to Gridview By Category and SubCategory in Asp.Net MVC Using
C#.Net, MVC
Pass Multiple Parameters in QueryString or URL in Asp.Net MVC, jQuery
Ajax Search and Display In MVC WebGrid in Asp.Net MVC Using C#.Net, Search
and Display Data In MVC WebGrid in Asp.Net MVC Using C#.Net, Bind
WebGrid With Entity Framework in Asp.Net MVC Using C#.Net.
So for this article first we will create a new asp.net mvc application and add a model class file and add the below code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication1.Models
{
public class StateModel
{
public
int StateId { get;
set; }
public
SelectList StateList { get; set; }
public
List<City>
CityList { get; set;
}
}
public class City
{
public
int CityId { get;
set; }
public
int StateId { get;
set; }
public
string CityName { get;
set; }
}
}
|
Now add controller class and the below code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
public
ActionResult Index()
{
StateModel objStateModel = new StateModel();
objStateModel.StateList =
State();
List<City> objcity = new List<City>();
objcity = GetCityList(0);
objStateModel.CityList = objcity;
return
View(objStateModel);
}
[HttpPost]
public
ActionResult Index(int StateId)
{
StateModel
objStateModel = new StateModel();
objStateModel.StateList =
State();
List<City> objcity = new
List<City>();
objcity = GetCityList(StateId);
objStateModel.CityList = objcity;
return
View(objStateModel);
}
public
SelectList State()
{
List<SelectListItem> items
= new List<SelectListItem>();
items.Add(new SelectListItem() { Text = "State
1", Value = "1",
Selected = true });
items.Add(new SelectListItem() { Text = "State
2", Value = "2",
Selected = false });
SelectList objstatelist = new SelectList(items, "Value",
"Text", 0);
return
objstatelist;
}
public
List<City> GetCityList(int stateID)
{
List<City> objCity = new List<City>();
objCity.Add(new City { CityId = 1, StateId = 1, CityName = "City 1" });
objCity.Add(new City { CityId = 2, StateId = 2, CityName = "City 2" });
objCity.Add(new City { CityId = 3, StateId = 1, CityName = "City 3" });
return
objCity.Where(m => m.StateId == stateID).ToList();
}
}
}
|
Now create view and add the below code into view page.
@model MvcApplication1.Models.StateModel
@{
ViewBag.Title = "DropdownList
AutoPostback Functionality In MVC Using C#.Net";
}
@using (Html.BeginForm("Index", "Home"))
{
@Html.DropDownListFor(model => model.StateId,
new SelectList(Model.StateList,
"Value", "Text"), new {
@onchange = "this.form.submit();"
})
<br />
<br />
<div>
<b> CITY AS PER STATE</b></div>
foreach (var item in
Model.CityList)
{
<div>
<b>City Name :</b>@item.CityName</div>
}
}
|
Now run the page to check the output.
0 comments:
Please let me know your view