【问题标题】:How to synchronize two DropDownList elements in MVC5?如何在 MVC5 中同步两个 DropDownList 元素?
【发布时间】:2014-09-06 00:00:32
【问题描述】:

我有类别和子类别作为模型。正如您可能已经猜到的那样,每个类别都包含许多子类别。关键是我有一个观点,在我看来我有一个像这样的代码段:

<div class="form-group">
    @Html.LabelFor(model => model.CategoryID, "Category", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList("CategoryID", null, "Please select a category",  htmlAttributes: new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.CategoryID, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.SubcategoryID, "Subcategory", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList("SubcategoryID", null, "Please select a subcategory", htmlAttributes: new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.SubcategoryID, "", new { @class = "text-danger" })
    </div>
</div>

然后,在我的控制器中,我有两个 ViewBag 对象,它们填充我的 SelectList 对象:

ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName");
ViewBag.SubcategoryID = new SelectList(db.Subcategories, "SubcategoryID", "SubcategoryName");

然后我写了一些 AJAX 代码,以使 DropDownList 元素的值同步。我有以下动作和JS代码。

[AllowAnonymous]
public JsonResult GetSubcategories(int id)
{
    var results = db.Subcategories.Where(s => s.CategoryID == id).Select(x => new { id = x.SubcategoryID, value = x.SubcategoryName }).ToList();

    return Json(results, JsonRequestBehavior.AllowGet);
}

带有 AJAX 调用的 JavaScript 代码:

$("#CategoryID").change(function () {
    $("#SubcategoryID").empty();

    $.ajax({
        type: 'POST',
        url: '/Account/GetSubcategories',
        dataType: 'json',
        data: { id: $("#CategoryID").val() },
        success: function (subcategories) {
            $.each(subcategories, function (i, subcategory) {
                $("#SubcategoryID").append('<option value="' + subcategory.value + '">' + subcategory.value + '</option>');
            });
        },
        error: function (ex) {
            console.log('Failed to retrieve subcategories! ' + ex);
        }
    });

    return false;
});

关键是,它同步了 DropDownLists,所以每当我选择一个类别时,我只显示该所选类别的子类别。但是,我现在无法提交我的表单,每当我按下提交时,我都会收到一条错误消息,指出所选值不是有效的子类别。我该如何解决?

编辑:

当我使用开发人员工具进行一些挖掘时,我看到对于我的类别,对于值部分,我有数字,这是它们在数据库中对应的 id,但现在对于我的值部分的子类别,我得到一个文本,表示子类别的名称。

【问题讨论】:

  • 您在提交表单之前看到错误消息了吗?
  • @TiesonT。不,视图中没有错误,提交表单之前的控制台中也没有。当我提交时,我收到错误消息,我看到了问题所在,请参阅我的编辑。我需要获取子类别的 id 部分。
  • 好的,解决了。我需要将部分 subcategory.value 更改为 subcategory.id。

标签: c# javascript asp.net ajax


【解决方案1】:

这个

$("#SubcategoryID").append('<option value="' + subcategory.value + '">' + subcategory.value + '</option>');

应该是

$("#SubcategoryID").append('<option value="' + subcategory.id + '">' + subcategory.value + '</option>');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    相关资源
    最近更新 更多