I have used Entity Framework to fetch the values and used Database First approach. I would also write an article on an Entity Framework but today, I would just show you how to bind the cascading dropdown list in this blog.
I have two dropdownlists. One is for state and the other for city. I would populate the city, which is based on the state selection. You can add as many dropdownlists, as you want. For simplicity, I am using only two dropdowns.
Create table scripts
tblState
- CREATE TABLE [dbo].[tblState](
- [stateid] [int] NOT NULL,
- [statename] [nvarchar](50) NULL,
- PRIMARY KEY CLUSTERED
- (
- [stateid] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
tblCity
- CREATE TABLE [dbo].[tblCity](
- [Cityid] [int] NOT NULL,
- [CityName] [nvarchar](50) NULL,
- [stateid] [int] NOT NULL
- ) ON [PRIMARY]
- GO
- ALTER TABLE [dbo].[tblCity] WITH CHECK ADD FOREIGN KEY([stateid])
- REFERENCES [dbo].[tblState] ([stateid])
- GO
I have tblState and tblCity, where I have stateid as Primary key in the tblState table and stateid as the Foreign key in tblCity table. You can insert states and cities from the database as you wish.
Now, with Entity Framework; create your EDMX file. In my case, I have named it ModelDemo.edmx.
Model
I have created a Model and named it Registration. This is not directly required but since I used a strongly typed View, so I have created Model. You can also add Model because if you write the httppost method to save the values to the database, then you would need the model.
- public class Registration
- {
- [Required(ErrorMessage = "Enter State")]
- public string State { get; set; }
- [Required(ErrorMessage = "Enter City")]
- public string City { get; set; }
- }
Controller
Now, this is my Controller code.
- [HttpGet]
- public ActionResult Details()
- {
- bindState();
- return View();
- }
- --------------------------------
- public void bindState()
- {
- var state = objEF.tblStates.ToList();
- List<SelectListItem> li = new List<SelectListItem>();
- li.Add(new SelectListItem { Text = "--Select State--", Value = "0" });
- foreach (var m in state)
- {
- li.Add(new SelectListItem { Text = m.statename, Value = m.stateid.ToString() });
- ViewBag.state = li;
- }
- }
- ----------------------------------
- public JsonResult getCity(int id)
- {
- var ddlCity = objEF.tblCities.Where(x => x.stateid == id).ToList();
- List<SelectListItem> licities = new List<SelectListItem>();
- licities.Add(new SelectListItem { Text = "--Select State--", Value = "0" });
- if (ddlCity != null)
- {
- foreach (var x in ddlCity)
- {
- licities.Add(new SelectListItem { Text = x.CityName, Value = x.Cityid.ToString() });
- }
- }
- return Json(new SelectList(licities, "Value", "Text", JsonRequestBehavior.AllowGet));
- }



Rohanjoy BhattacharyaPosted Jun 4, 2018, 4:13 AM
I have used strongly typed view. And my model class is the registration model.
Yasin ShPosted Jun 4, 2018, 3:55 AM
What are the models you used in View Details?