我正在使用最新版本的jQuery和带有Razor视图引擎的ASP.NET MVC 3。
我尝试通过Google寻找一个合适的示例来加载子下拉列表,当选择父下拉项时。我想通过jQuery
AJAX
并使用JSON
来实现这一点。我的了解是零。
我有一个类别类,其中包含一系列类别。这是一个父子关联关系。
如果我从父下拉列表中选择一个类别,则需要在所选父类别的子下拉列表中列出所有子类别。
这是我目前拥有的内容,但需要完善,不确定我是否朝着正确的方向:
$(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" })
第一个选项有"--选择--"的文本(对于父和子下拉列表)。在初始页面加载时,子下拉列表中不必加载任何内容。当选择值时,子下拉列表必须被填充。当再次在父下拉列表中选择"--选择--"时,必须清除子下拉列表中的所有项目,除了"--选择--"。
如果可能,如果正在加载子类别,我该如何显示那个“圆形”加载图标?
更新
我已经更新了我的代码到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