0

I'm working with MVC 4.0 and I'd need a page with 3 drop downlists and a table.

When an item is selected from DDL1 (e.g. Users) then DDL2 (e.g. Accounts) is populated. When an item is selected from DDL2 (Accounts) then DDL3 (e.g. Cards) is populated. When an item is selected from DDL3 then the table has to display a list of objects e.g. Transactions (with Amount, etc).

So I created a TransactionManagementController and created an Index Action which loads the Users and displays them initially.

 public ActionResult Index()
        {
            var vm = new VmTransactionManagement
                {
                    Users = db.Users.ToList()
                };

            return View(vm);
        }

<th>User:</th>
        <td>@Html.DropDownListFor(m => m.SelectedUser,
                                  Model.UsersSelectList,
                                  "-- Select One --")</td>

Also, I created a view model for this controller: VmTransactionManagement which contains some other objects needed.

So, now how would I code so that when DDL1 is selected (userId is passed), then DDL2 gets populated?

Should I be creating a new action/view and calling it by Ajax?

If I create a new Action, then I'll have to create a new View which changes the previously selected values.

tereško
  • 58,060
  • 25
  • 98
  • 150
The Light
  • 26,341
  • 62
  • 176
  • 258

1 Answers1

0

When I need to cascade drop down lists, I will use jQuery $.load and render the PartialView. An example using technique is at https://stackoverflow.com/a/1721623/1015010

Community
  • 1
  • 1
da7rutrak
  • 548
  • 3
  • 13
  • What should the partial view contain? the second drop down list populated based on the selected if from the first drop down list? – The Light Feb 22 '13 at 16:11
  • Yes, that's exactly it. You can pass data using `$.load` to the controller (such as the user ID, or whatever) and return proper choices based on that selection in the partial view. – da7rutrak Feb 22 '13 at 16:17