MVC 3 级联下拉列表

发布于 2024-11-24 02:00:09 字数 1615 浏览 1 评论 0原文

我不知道如何准确地拥有级联 DropDownLists

我的情况是下一个:

类别有项目,项目的数量取决于建立

我想要有两个 DropDownLists,您选择一个类别,下一个在您选择时填充第一个包含该类别中的项目,当您选择项目时,将显示一个包含每个机构数量的表格。

好吧,这将是我的 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();
    }

,那么我将有两个 DropDownList,它们应该将值发布到 Item_ID 和 Category_ID ...第一个类别,然后是项目

@Html.DropDownList("Categories", new SelectList((IQueryable<Inventario_Data.Models.Category>)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

I don't know how to exactly have Cascading DropDownLists

My scenario is the next:

Category has Items and Items have quantities depending on Establishment

I want to have two DropDownLists one which you select a Category, next one is populated when you make a selection of the first with the Items in that Category, and when you select the Item a table with the quantities for each establishment is shown.

Ok this would be my 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();
    }

then, I would have my two DropDownLists that should post values to Item_ID and Category_ID ... first category then item

@Html.DropDownList("Categories", new SelectList((IQueryable<Inventario_Data.Models.Category>)ViewData["Categories"], "ID", "Name", ViewBag.Category_ID), "Select an Item Category", new { onchange = "window.location.href = '/Inventory/ItemByClinic/Categody_ID=' + this.value" })

This is what I don't know how to do ... how should I put the URL or how should I send it, so when I send the other ID does'nt mix up and I can receive my IDs

How do I receive the values of each DropDownList in the ActionResult? how should they be sent?

ANSWER

I found the answer from this website, just wanted to let know what I did

http://kmsystems.squarespace.com/journal/2009/5/31/aspnet-mvc-cascading-dropdownlists.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咆哮 2024-12-01 02:00:09

您描述问题的方式听起来像是您试图同时做太多事情。

为了更容易解释,我将使用国家/州查找用例。 (当我选择“国家/地区”时,将填充“州/省”下拉列表。)

您有 4 个元素:

  1. 初始表单加载(无国家/地区,未选择州)
  2. 已选择国家/地区,已填充州/省/自治区/直辖市已选择国家/地区
  3. ,已选择州/省/自治区
  4. 错误处理(无效的国家/地区和州) ;状态组合)

当我遇到这个时,我处理步骤1和; 3 与您的示例类似的视图。

那么您是否陷入了第 2 步?当您说“我应该如何放置 URL 或如何发送它”时,您的意思是什么?

对我来说,我将通过创建 javascript 控制器并使用 jquery 来发布和发送来解决第 2 步。返回选择国家/地区下拉框时触发的 json 对象。

我找到了 MVC Music Store书呆子晚餐示例非常有帮助。

如果您需要 json/jquery 的示例,请参阅音乐商店示例中的购物车。

The way you are describing your problem sounds like you are trying to do too many things at once.

To make it easier to explain, I'm going to use the Country / State lookup use case. (When I select "Country", the "State" drop down is populated.)

You have 4 elements:

  1. Initial form load (no country, no state selected)
  2. Country selected, State populated
  3. Country selected, State selected
  4. Error handling (invalid Country & State combination)

When I have come across this, I handle Step 1 & 3 in a view similar to your example.

So are you getting stuck on step 2? What do you mean when you say " how should I put the URL or how should I send it,"

For me, I'll address step 2 by creating javascript controller and use jquery to post & return json objects triggered when the Country dropdown box is selected.

I found the MVC Music Store and Nerd Dinner examples to be extremely helpful.

If you need an example of the json / jquery, see the shopping cart in The Music Store example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文