Cascading DropDown List in MVC 4

Posted by Misi on Stack Overflow See other posts from Stack Overflow or by Misi
Published on 2012-06-29T10:24:17Z Indexed on 2012/12/11 23:04 UTC
Read the original article Hit count: 269

I have a ASP.NET MVC 4 project with EF I have a table with Parteners. This table has 2 types of parteners : agents(part_type=1) and clients(part_type=2). In an Create view I have the first DropDownList that shows all my agents, a button and the second DDL that shows all my clients that correspond to the selected agent. Q1 : What button shoud I use ? , , @Html.ActionLink() ? Create.cshtml

    <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");//
 }

Q: How can I pass the selected agent id from the first DDL to my controller ?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about asp.net-mvc