Click here to Skip to main content
15,072,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am having 2 tables city and state i want to get state wise city in dropdownlist , i have already got both tables data in output in dropdownlist but state wise city is not working , i have also given foreign key, i want to to do this thing in mvc5,

What I have tried:

i have done my both tables with given foreign key
getting both DDl's data in output
but i cant get it state wise city

i have tried it using jscrpit but not worked

here is my controler code:-

namespace Web4.Controllers
{
    public class AccountController : Controller
    {
        private void PopulateLookups(eBusPassSystemEntities db)
        {
            IEnumerable<SelectListItem> item = db.tbl_state.Select(C => new SelectListItem
            {
                Value = C.state_name,
                Text = C.state_name
            }).ToList(); 

            ViewBag.state = item;

            IEnumerable<SelectListItem> itenn = db.tbl_city.Select(C => new SelectListItem
            {
                Value = C.city_name,
                Text = C.city_name
            }).ToList();

            ViewBag.city = itenn;

        }
       


        [HttpGet]
        public ActionResult register(tbl_state t)
        {
            using (eBusPassSystemEntities db = new eBusPassSystemEntities())
            {
                PopulateLookups(db);
                
            }

            return View();

           
        }

        [HttpPost]
        public ActionResult Register(tbl_user u)
        {
            if (!ModelState.IsValid)
            {
                using (eBusPassSystemEntities db = new eBusPassSystemEntities())
                {
                    PopulateLookups(db);
                }

                return View();
            }

            using (eBusPassSystemEntities Db = new eBusPassSystemEntities())
            {
                Db.tbl_user.Add(u);
                Db.SaveChanges();
            }

            TempData["message"] = "Hello" + u.name + " Your Account Has been Succesfully created.";
            return RedirectToAction("Success");
        }

        [HttpGet]
        public ActionResult Success()
        {
            ViewBag.message = TempData["message"];
            return View();
        }
    }
}
Posted
Updated 25-Feb-17 19:32pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900