[英]How to populate a dropdownlist by an action from another drop down list?
I'm working with MVC 4.0 and I'd need a page with 3 drop downlists and a table. 我正在使用MVC 4.0,并且需要一个包含3个下拉列表和一个表的页面。
When an item is selected from DDL1 (eg Users) then DDL2 (eg Accounts) is populated. 当从DDL1(例如,用户)中选择一个项目时,将填充DDL2(例如,帐户)。 When an item is selected from DDL2 (Accounts) then DDL3 (eg Cards) is populated.
当从DDL2(帐户)中选择一个项目时,将填充DDL3(例如卡)。 When an item is selected from DDL3 then the table has to display a list of objects eg Transactions (with Amount, etc).
当从DDL3中选择一个项目时,表格必须显示一个对象列表,例如,交易(带有金额等)。
So I created a TransactionManagementController and created an Index Action which loads the Users and displays them initially. 因此,我创建了一个TransactionManagementController并创建了一个Index Action,该Action加载用户并最初显示它们。
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. 另外,我为此控制器创建了一个视图模型:VmTransactionManagement,其中包含一些其他需要的对象。
So, now how would I code so that when DDL1 is selected (userId is passed), then DDL2 gets populated? 那么,现在我该如何编码,以便在选择DDL1(传递了userId)之后填充DDL2?
Should I be creating a new action/view and calling it by Ajax? 我应该创建一个新的动作/视图并由Ajax调用吗?
If I create a new Action, then I'll have to create a new View which changes the previously selected values. 如果我创建一个新的动作,则必须创建一个新的视图来更改以前选择的值。
When I need to cascade drop down lists, I will use jQuery $.load
and render the PartialView. 当我需要级联下拉列表时,我将使用jQuery
$.load
并渲染PartialView。 An example using technique is at https://stackoverflow.com/a/1721623/1015010 使用技术的示例位于https://stackoverflow.com/a/1721623/1015010
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.