Wednesday, 25 November 2015

DropdownList AutoPostback Functionality In MVC Using C#.Net

11/25/2015 - By Pranav Singh 0

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.


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.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top