0

Is it possible to use cascading dropdownlist in ASP.Net MVC scaffolding? If yes, how?

I have here some of the codes that will show the dropdownlist using scaffolding:

MyController.cs

// GET: /ManualEntries/Create

public ActionResult Create()
{
    ViewBag.cluscd = new SelectList(db.CLUSTERs, "clus_id", "clusdesc");
    ViewBag.seccd = new SelectList(db.SECTORs, "sec_id", "secdesc");
    return View();
}

Create.cshtml

<div class="form-group">
    @Html.LabelFor(model => model.sec_id, "Sector", new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList("sec_id", String.Empty)
        @Html.ValidationMessageFor(model => model.sec_id)
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.clus_id, "Cluster", new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList("clus_id", String.Empty)
        @Html.ValidationMessageFor(model => model.clus_id)
    </div>
</div>
ekad
  • 14,436
  • 26
  • 44
  • 46
  • 1
    refer this => http://stephenwalther.com/archive/2008/09/07/asp-net-mvc-tip-41-creating-cascading-dropdown-lists-with-ajax – cracker Jan 08 '15 at 05:45

1 Answers1

0

You can do like this

Controller

List<SelectListItem> selectlist = new List<SelectListItem>();
//fill the list and assign to viewbag and than access in view
ViewBag.SelectList = selectlist;

View

@Html.DropDownList("name",(IEnumerable<SelectListItem>)ViewBag.SelectList)
Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40