2012-09-29 64 views
0

我有3个DropDownLists;当我在#1中选择一个项目时,我希望根据#1的值填充#2中的项目。当我在#2中选择一个项目时,#3中的项目应该根据#2中的选择填充。它们都位于名为GetItemsForLeve1的表单中。我已经开始使用下拉式的onchange链接Razor的项目DropDownLists

<% using (Html.BeginForm("GetItemsForLeve1", "Index")) 
    { %>    
     Main Group: <%:Html.DropDownList("Level1", (SelectList)ViewBag.Level1, "Select One", new { onchange = "$this.form.submit()" })%> 
     <%:Html.DropDownList("Level2", (SelectList)ViewBag.Level2, "-", new { onchange = "$this.form.submit()" })%> 
     <%:Html.DropDownList("Level3", (SelectList)ViewBag.Level3, "-", new { onchange = "$this.form.submit()" })%> 
     <input type="submit" value="Filter" /> 
    <%}%> 
  1. 是否有可能来填补2级和3级下拉列表不发送页面返回到服务器?

  2. 如何判断在GetItemsForLevel操作中点击了哪个下拉列表? 我对MVC完全陌生,所以我很欣赏以简单的方式告诉我?

谢谢

+0

这可能是你要找的http://stackoverflow.com/a/4326151/713149 –

回答

0

据我知道,真的有没有,这是否对你的组件。但是你可以使用Ajax.BeginForm帮手来构建它。

  1. 第一AJAX形式应当包含在第一选择列表,并发送回一个返回部分视图的动作
  2. 1中的局部视图应该与第二选择列表
  3. 返回第二个AJAX形式
  4. 等等

所以主要Razor视图应该包含这样的事情:

@using (Ajax.BeginForm("SecondAjaxForm", new AjaxOptions() { UpdateTargetId = "secondFormDiv" }) 
{ 
    @Html.DropDownList("selectList1", Model.FirstSelectList, new { onchange = "this.form.submit()") 
} 

<!-- the second AJAX form + drop-down list will get populated here 
<div id="secondFormDiv"></div> 

SecondAjaxForm动作

public ActionResult SecondAjaxForm(string selectList1) 
{ 
    SelectList secondSelectList; 
    // populate the second select list here 
    return PartialView("SecondAjaxForm", secondSelectList); 
} 

局部视图SecondAjaxForm应基本与上述相同的第一种形式。

@model SelectList 
@using (Ajax.BeginForm("ThirdAjaxForm", new AjaxOptions() { UpdateTargetId = "thirdFormDiv" }) 
{ 
    @Html.DropDownList("selectList2", Model, new { onchange = "this.form.submit()") 
} 

<!-- the third AJAX form + drop-down list (if any) will get populated here 
<div id="thirdFormDiv"></div>