3

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

sergioadh
  • 1,461
  • 1
  • 16
  • 24
  • Care to share some code? You can do this via jQuery-Ajax btw. – Alex R. Jul 14 '11 at 04:57
  • Well the thing is that I don't know where to start ... I have two DropDownLists .. which one depends on the other .. and I can get the values by placing them on the ViewData on code-behind, but in code-behind in the ActionResult I don't know how to retrieve the ID's of both of the DropDownLists ... – sergioadh Jul 14 '11 at 05:01
  • So you _do_ have some code. Post it here so we can see what is causing you the trouble. – Alex R. Jul 14 '11 at 05:04

1 Answers1

2

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.

Christian Payne
  • 7,081
  • 5
  • 38
  • 59
  • Yes. that's the case, I'm gonna read the Music Store PDF thanks, i'll post back here if I find the solution – sergioadh Jul 14 '11 at 07:00
  • I haven't found the solution in MVC Music Store, they show multiple DropDownLists but they're not how I need them to be, as you have mentioned. Could you show me how would you address step two, then step 3 with jQuery and how the ActionResult receives the values? – sergioadh Jul 14 '11 at 17:04
  • I found the answer here http://kmsystems.squarespace.com/journal/2009/5/31/aspnet-mvc-cascading-dropdownlists.html but you were right .. I had to use JSON / jQuery – sergioadh Jul 15 '11 at 00:13