Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am populating two Dropdown lists (Category and SubCategory) in my view.

I am doing that thru my controller, and i want the Dropdown of SubCategory to be populated depending on the selected item from Category.

So i have made the Controller and View, but i need now Ajax script to make this work. Can anyone help me with suggestions.

Controller:

[HttpGet]
        public ActionResult Edit(int id)
        {
            //var model = dc.Entry.FirstOrDefault(e => e.Id == id);

            EntryViewModel model = (from subcat in dc.SubCategory
                                    join en in dc.Entry on subcat.SubCategoryId equals en.SubCategoryId
                                    join cat in dc.Category on subcat.CategoryId equals cat.Id
                                    where en.Id == id
                                    select new EntryViewModel {
                                        Id = en.Id,
                                        Title = en.Title,
                                        Username = en.Username,
                                        Password = en.Password,
                                        Url = en.Url,
                                        Description = en.Description,
                                        CategoryId = cat.Id,
                                        CategoryName = cat.Name,
                                        SubCategoryId = subcat.SubCategoryId,
                                        SubCategoryName = subcat.Name
                                    }).First();

            string selectedCat = (from cat in dc.Category
                                  join en in dc.SubCategory on cat.Id equals en.SubCategoryId
                                  where cat.Id == en.SubCategoryId
                                  select cat.Name).First();

            ViewBag.SubjectNameCat = new SelectList(dc.Category, "Id", "Name");

            return View(model);
}


   public JsonResult SubCategories(int id)
        {
            string selected = (from cat in dc.SubCategory
                               join en in dc.Entry on cat.SubCategoryId equals en.SubCategoryId
                               where en.Id == id
                               select cat.Name).First();

            return Json(new SelectList(selected.ToArray(), "SubCategoryId", "Name"), JsonRequestBehavior.AllowGet);
        }


View:

<div class="editor-label">
               Category
           </div>
           <div class="editor-field">
            @Html.DropDownListFor(model => model.CategoryId
                            (IEnumerable<SelectListItem>)ViewBag.SubjectNameCat,
                                         new { @class = "form-control" })
           </div>
Posted

Cascading Dropdown List With MVC, LINQ to SQL and AJAX[^]

This is a very nice article in code project by Sandeep. Please check this.
Hope this helps.
thanks
:)
 
Share this answer
 
Comments
PetarS089 29-Oct-14 5:20am    
Thanks for the link you provided. It was very helpful.
[no name] 29-Oct-14 5:21am    
Anytime Petar..Happy to help u..:)

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