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

認真嗎?沒有評論的投票爲什麼? –