【问题标题】:Dropdown onchange load another dropdown MVC4下拉 onchange 加载另一个下拉 MVC4
【发布时间】:2014-10-28 20:45:54
【问题描述】:

在我看来,我正在填充两个下拉列表(类别和子类别)。

我正在通过我的控制器执行此操作,并且我希望根据从 Category 中选择的项目来填充 SubCategory 的下拉列表。

任何建议如何做到这一点,是否有更好和更有效的方式来填充下拉列表?

Controller:

public ActionResult Edit(int 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", selectedCat);

        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();

        ViewBag.SubjectName = new SelectList(dc.SubCategory, "SubCategoryId", "Name", selected);

        return View(model);
    }

查看:为简单起见,仅显示下拉列表

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

    <div class="editor-label">
        SubCategory
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.SubCategoryId, (IEnumerable<SelectListItem>)ViewBag.SubjectName,
               new { @class = "form-control" })
    </div>

【问题讨论】:

  • 谷歌mvc cascading dropdownlist。有很多例子可以说明如何做到这一点。作为旁注,SelectList 代码的最后一个参数毫无意义。下拉列表的选定值将是 model.CategoryIdmodel.SubCategoryId 的值

标签: asp.net-mvc entity-framework asp.net-mvc-4


【解决方案1】:

@PetarS 试试这个最适合我的很棒的插件。 https://github.com/rajkaimal/CascadingDropDown

【讨论】:

    猜你喜欢
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 2019-10-15
    相关资源
    最近更新 更多