【发布时间】:2011-08-08 14:45:30
【问题描述】:
我正在使用最新版本的 jQuery 和 ASP.NET MVC 3 以及 Razor 视图引擎。
我曾尝试 Google 寻找一个在选择父下拉项时加载子下拉列表的体面示例。我希望通过jQuery AJAX 使用JSON 来做到这一点。我对此的了解为零。
我有一个包含类别列表的 Category 类。这是一个父子关联。
如果我从父下拉列表中选择一个类别,则所有子类别都需要在所选父类别的子下拉列表中列出。
这是我目前拥有的,但需要完成它,不确定我是否走对了方向:
$(document).ready(function () {
$('#ddlParentCategories').change(function () {
alert('changed');
});
});
我从我的视图模型中加载了我的下拉列表:
@Html.DropDownListFor(x => x.ParentCategoryId, new SelectList(Model.ParentCategories, "Id", "Name", Model.ParentCategoryId), "-- Select --", new { id = "ddlParentCategories" })
第一项具有文本“-- Select --”(对于父级和子级下拉列表)。在初始页面加载时,必须在子下拉列表中加载任何内容。选择一个值后,必须填充子下拉列表。而当在父下拉列表中再次选择“-- Select--”时,子下拉列表中的所有项目都必须清除,除了“-- Select--”。
如果可能,如果子类别正在加载,我如何显示那个“圆形”加载图标?
更新
我已将我的代码更新为 Darin 的代码,但我无法使其正常工作:
Category类:
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string MetaKeywords { get; set; }
public string MetaDescription { get; set; }
public bool IsActive { get; set; }
public int? ParentCategoryId { get; set; }
public virtual Category ParentCategory { get; set; }
public virtual ICollection<Category> ChildCategories { get; set; }
}
EditProductViewModel类:
public class EditProductViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public string ShortDescription { get; set; }
public string LongDescription { get; set; }
public bool IsActive { get; set; }
public string PageTitle { get; set; }
public bool OverridePageTitle { get; set; }
public string MetaKeywords { get; set; }
public string MetaDescription { get; set; }
public int ParentCategoryId { get; set; }
public IEnumerable<Category> ParentCategories { get; set; }
public int ChildCategoryId { get; set; }
public IEnumerable<Category> ChildCategories { get; set; }
}
ProductController类:
public ActionResult Create()
{
EditProductViewModel viewModel = new EditProductViewModel
{
ParentCategories = categoryService.GetParentCategories()
.Where(x => x.IsActive)
.OrderBy(x => x.Name),
ChildCategories = Enumerable.Empty<Category>(),
IsActive = true
};
return View(viewModel);
}
public ActionResult AjaxBindingChildCategories(int parentCategoryId)
{
IEnumerable<Category> childCategoryList = categoryService.GetChildCategoriesByParentCategoryId(parentCategoryId);
return Json(childCategoryList, JsonRequestBehavior.AllowGet);
}
Create查看:
<tr>
<td><label>Parent Category:</label> <span class="red">*</span></td>
<td>@Html.DropDownListFor(x => x.ParentCategoryId,
new SelectList(Model.ParentCategories, "Id", "Name", Model.ParentCategoryId),
"-- Select --",
new { data_url = Url.Action("AjaxBindingChildCategories"), id = "ParentCategories" }
)
@Html.ValidationMessageFor(x => x.ParentCategoryId)
</td>
</tr>
<tr>
<td><label>Child Category:</label> <span class="red">*</span></td>
<td>@Html.DropDownListFor(x => x.ChildCategoryId,
new SelectList(Model.ChildCategories, "Id", "Name", Model.ChildCategoryId),
"-- Select --",
new { id = "ChildCategories" }
)
@Html.ValidationMessageFor(x => x.ChildCategoryId)
</td>
</tr>
<script type="text/javascript">
$(document).ready(function () {
$('#ParentCategories').change(function () {
var url = $(this).data('url');
var data = { parentCategoryId: $(this).val() };
$.getJSON(url, data, function (childCategories) {
var childCategoriesDdl = $('#ChildCategories');
childCategoriesDdl.empty();
$.each(childCategories, function (index, childCategory) {
childCategoriesDdl.append($('<option/>', {
value: childCategory, text: childCategory
}));
});
});
});
});
</script>
它进入我的 AjaxBindingChildCategories 操作并带回记录,它只是不想显示我的子类别下拉列表。我查看了 Fire Bug,得到的错误是:
GET AjaxBindingChildCategories?parentCategoryId=1
500 Internal Server Error
【问题讨论】:
-
您所询问的内容称为“级联下拉菜单”。这将在谷歌搜索时有所帮助。
-
好的,谢谢,让我去进一步搜索:)
标签: ajax asp.net-mvc-3 jquery razor