2012-08-16 89 views
0

在我的MVC3应用程序中,我在视图中有两个下拉菜单“Category”和“SubCategory”,它们来自两个不同的模型。视图本身属于第三种模式。我正在使用NHibernate for ORM。不同模型的级联下拉

我的问题是如何级联两个下拉。子类别下拉菜单应显示值取决于在类别下拉列表中选择的值。 “类别”下拉菜单从“类别”模型中检索值,“子类别”下拉菜单从“子类别”模型中检索值。这两个下拉列表显示在属于另一个模型“费用”的视图中。

请查看此段代码以获取更多信息。支出观点有以下下拉菜单:

@using (Html.BeginForm()) { 
<div class="editor-field"> 
      @Html.DropDownListFor(model => model.CategoryId.CategoryName, new SelectList(new MyExpense.Persistence.Repositories.CategoriesRepository().GetCategoryName())) 
      @Html.ValidationMessageFor(model => model.CategoryId.CategoryName) 
</div> 
<div class="editor-field"> 
     @Html.DropDownListFor(model => model.SubCategoryId.SubCategoryName, new SelectList(new MyExpense.Persistence.Repositories.SubCategoriesRepository().GetSubCategoryName())) 
     @Html.ValidationMessageFor(model => model.SubCategoryId.SubCategoryName) 
</div> 
} 

CategoriesRepository和SubCategoriesRepository是类包含从数据库中检索类别和SuCategories方法。

任何帮助,这是非常感谢。

回答

0

就个人而言,我只会填充第一个下拉列表,然后在选择更改时执行AJAX请求 - 请求所选类别标识的子类别选项。从你回来的结果你可以重建子类别列表。退房this post

+0

感谢您的回复!我确实尝试了建议的解决方案,但SubCategory下拉列表为空,不显示任何内容。当我从类别dropwdown中选择值时,它会引发一个异常,因为“ViewData未提供关键的CategoryId.CategoryName”。我在Expense Conyroller中做了popualte ViewData,作为, ViewData [“CatgeoryId.CategoryName”] = CategoryClass.GetAllCategories(); 有关这个的任何想法? – shw 2012-08-20 09:19:05

+0

你可以发布你的点击处理程序的类别下拉列表的JS,我会看看 – MiiisterJim 2012-08-21 16:31:04