1

嗨,我在 mvc 3 中使用带有级联下拉列表的 ajax。这是代码:

<script>
function LoadSubCat() {
    var des = $("#CategoryID").val();

    // Call our controller method and process the list of Model objects
    $.getJSON('@Url.Content("~/ProjectController/GetSubCategory")', { Category_ID: des },
        function (subCategory) {
            $("#SubCategory").empty();
            $.each(subCategory, function (i, c) {
                $("#SubCategory").append(
                    $('<option></option>').val(c.id).html(c.name)
                );
            });
    });
}
</script>

看法

<div class="editor-field">
    @Html.DropDownList("CategoryID", String.Empty)
    @Html.ValidationMessageFor(model => model.CategoryID)
</div>
 <div class="editor-label">
    SubCategory
</div>
<div class="editor-field">
    <select id="SubCategory" onchange="LoadSubCat()"></select>
</div>

控制器

public ActionResult Create()
{
    ViewBag.CategoryID = new SelectList(db.pjt_Categories, "pjt_Category_ID", "CatName");
    ViewBag.Status = new SelectList(db.pjt_Statuses, "pjt_Status_ID", "StatusName");

    return View();
}

public JsonResult GetSubCategory(string Category_ID)
{
    // Instantiate our context and do whatever goo we need to select the objects we want
    using (laintranet1Entities ctx = new laintranet1Entities())
    {
        return Json(ctx.pjt_SubCategories.Where(d => d.CategoryID == Category_ID).ToList(), JsonRequestBehavior.AllowGet);
    }
}

我不知道我哪里出错了,有人可以帮助我,因为我必须在今晚之前完成这个。任何帮助都会很棒,谢谢。

4

0 回答 0