MVC 3 Cascading DropDownLists
我不知道如何准确地拥有Cascading DropDownLists
我的情景是下一个:
类别有物品和物品的数量取决于企业
我希望有两个DropDownLists,您选择一个Category,当您选择第一个包含该Category中的Items时,将填充下一个DropDownLists,当您选择Item时,将显示包含每个建立的数量的表。
好的,这将是我的ActionResult
public ActionResult ItemByClinic(Guid? Item_ID, Guid? Category_ID) { ViewData["Categories"] = InventoryDb.Categories; if (Category_ID != null) { ViewBag.Category_ID = Category_ID; ViewData["Items"] = InventoryDb.Items.Where(i => i.Category.ID == Category_ID); if (Item_ID != null) { ViewBag.Item_ID = Item_ID; ViewData["Inventory"] = InventoryDb.Items.Single(i => i.ID == Item_ID).Inventory; } } return View(); }
然后,我会让我的两个DropDownLists应该将值发布到Item_ID和Category_ID …第一个类别然后是项目
@Html.DropDownList("Categories", new SelectList((IQueryable)ViewData["Categories"], "ID", "Name", ViewBag.Category_ID), "Select an Item Category", new { onchange = "window.location.href = '/Inventory/ItemByClinic/Categody_ID=' + this.value" })
这是我不知道该怎么做…我应该如何设置URL或者我应该如何发送它,所以当我发送其他ID时不会混淆我可以收到我的ID
如何在ActionResult中接收每个DropDownList的值? 应该怎么发送?
回答
我从这个网站找到答案,只是想知道我做了什么
http://kmsystems.squarespace.com/journal/2009/5/31/aspnet-mvc-cascading-dropdownlists.html
你描述问题的方式听起来像是你想要一次做太多事情。
为了便于解释,我将使用Country / State查找用例。 (当我选择“国家/地区”时,会填充“状态”下拉列表。)
你有4个要素:
- 初始表单加载(没有国家/地区,没有选择状态)
- 选择国家,州人口稠密
- 选择国家,选择国家
- error handling(无效的国家和州组合)
当我遇到这个问题时,我会在类似于您的示例的视图中处理步骤1和3。
所以你是否陷入第2步? 当你说“我应该如何放置URL或我应该如何发送它”时,你的意思是什么?
对我来说,我将通过创建javascript控制器来解决第2步,并使用jquery发布并返回选中Country下拉框时触发的json对象。
我发现MVC音乐商店和书呆子晚餐的例子非常有帮助。
如果您需要json / jquery的示例,请参阅音乐商店示例中的购物车。