如何在MVC4中制作两个级联下拉列表 [英] How to make two cascading dropdown lists in MVC4

查看:77
本文介绍了如何在MVC4中制作两个级联下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的视图中填充了两个下拉列表(Category和SubCategory)。



我通过我的控制器这样做,我希望SubCategory的Dropdown根据从Category中选择的项目进行填充。



所以我已经制作了Controller和View,但我现在需要Ajax脚本来完成这项工作。任何人都可以帮我提出建议。



控制器:



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>

推荐答案

级联下拉列表,包含MVC,LINQ to SQL和AJAX [ ^ ]



这是Sandeep在代码项目中的一篇非常好的文章。请检查一下。

希望这会有所帮助。

谢谢

:)
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
:)


添加更多链接检查,



http://www.c-sharpcorner.com/UploadFile/4d9083/creating-simple-cascading-dropdownlist-in-mvc-4-using-razor/ [< a href =http://www.c-sharpcorner.com/UploadFile/4d9083/creating-simple-cascading-dropdownlist-in-mvc-4-using-razor/target =_ blanktitle =New Window > ^ ]



http://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx [ ^ ]



https://code.msdn.microsoft.com/Cascading-DropDownList-in-833683f9 [ ^ ]
Adding some more links to check ,

http://www.c-sharpcorner.com/UploadFile/4d9083/creating-simple-cascading-dropdownlist-in-mvc-4-using-razor/[^]

http://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx[^]

https://code.msdn.microsoft.com/Cascading-DropDownList-in-833683f9[^]


这篇关于如何在MVC4中制作两个级联下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆