2

我有一个ASP.NET MVC 4项目与EF 我有一个表Parteners。此表有两种类型的parteners:代理(part_type = 1)和客户端(part_type = 2)。 在创建视图中,我有第一个显示所有我的代理的DropDownList,一个按钮和第二个DDL,显示与选定代理对应的所有客户端。 Q1:我用什么按钮? ,@ Html.ActionLink()? Create.cshtmlMVC 4级联DropDown列表

<div class="editor-field"> 
     @Html.DropDownList("idagenti", ViewData["idagenti"] as List<SelectListItem>, String.Empty) 
    </div> 
    @*a button*@ 
    <div class="editor-label"> 
     @Html.LabelFor(model => model.id_parten, "Client") 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("id_parten", String.Empty) 
     @Html.ValidationMessageFor(model => model.id_parten) 
    </div> 

OrdersController.cs

public ActionResult Create(int? id) // id is the selected agent 
{ 
    var agqry = db.partener.Where(p => p.part_type == 1).Where(p => p.activ == true); 
    var cltqry = db.partener.Where(p => p.part_type == 2).Where(p => p.activ == true); 
    List<SelectListItem> idagenti = new List<SelectListItem>(); 
    foreach (partener ag in agqry) 
    { 
     idagenti.Add(new SelectListItem { Text = ag.den_parten, Value = ag.id_parten.ToString() }); 
    } 
    if (id != null) 
    { 
     cltqry = cltqry.Where(p => p.par_parten == id); 
    } 
    ViewData["idagenti"] = idagenti; 
    ViewBag.id_parten = new SelectList(cltqry, "id_parten", "den_parten");// 
} 

问:如何传递从第一DDL选定代理人身份证到我的控制器?

+0

http://blogs.msdn.com/b/rickandy/archive/2012/01/09 /cascasding-dropdownlist-in-asp-net-mvc.aspx – RickAndMSFT

+0

可能的重复:http://stackoverflow.com/questions/705540/asp-net-mvc-cascading-drop-down –

+0

[与jQuery MVC AJAX级联DropDown列表](HTTP:// lesson8 .blogspot.com/2013/07/cascading-dropdown-lists-with-jquery.html) – Sender

回答

6

以下表格是根据选定的性别(男性或女性)显示性别的标题(男性先生,女性女士)的情况。

使用Ajax.Begin()助手,您可以回发给控制器并将值返回给视图。

所有数据都是硬编码的,请原谅手动添加信息。

视图 - Form.cshtml

<fieldset> 
    <legend>Form</legend> 
    @* This will post to the BindTitles method in the Form Controller *@ 
    @using (Ajax.BeginForm("BindTitles", "Form", new AjaxOptions 
    { 
     HttpMethod = "POST" 
    })) 
    { 
     <p> 
      @Html.DropDownList("Genders") 
     </p>   
     <p> 
      <input type="submit" value="Submit" /> 
     </p> 
    } 
    <p> 
     @Html.DropDownList("Titles") 
    </p> 
</fieldset> 

控制器 - 的FormController

public ActionResult Form() 
    { 
     List<string> genderList = new List<string>(); 
     genderList.Add("Male"); 
     genderList.Add("Female"); 
     ViewBag.Genders = new SelectList(genderList); 
     ViewBag.Titles = new SelectList(new List<string>()); 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult BindTitles(string genders) 
    { 
     List<string> titles = new List<string>(); 
     if (genders == "Male") 
     { 
      titles.Add("Mr."); 
      titles.Add("Sr."); 
     } 
     else 
     { 
      titles.Add("Ms."); 
      titles.Add("Mrs."); 
     } 
     ViewBag.Titles = new SelectList(titles); 
     List<string> genderList = new List<string>(); 
     genderList.Add("Male"); 
     genderList.Add("Female"); 
     ViewBag.Genders = new SelectList(genderList); 
     return View("Form"); 
    } 
+2

认真吗?没有评论的投票为什么? –