Search code examples
c#asp.net-mvcasp.net-mvc-4drop-down-menucascadingdropdown

Cascading update of two @Html.DropDownListFor in MVC4 with model binding


I saw this, however I have a different Q. I have a view like this:

@model myPrj.Models.RollCallModel
...
<table>
    <tr> //Master DDL
       <td>
          @Html.LabelFor(model => model.CourseID) 
          @Html.DropDownListFor(model => model.CourseID,
            new SelectList(new myPrj.Models.myDbContext().Courses
               .Where(c => c.StatusID == 0), "ID", "ID"), 
            "choose...", new { id = "ddlCourse"})
          @Html.ValidationMessageFor(model => model.CourseID)
       </td>
     </tr>
     <tr> //Detail DDL
        <td>
            @Html.LabelFor(model => model.PersonnelID) 
            @Html.DropDownListFor(model => model.PersonnelID,
                 null, "choose another...", new { id = "ddlPersonnel"})
            @Html.ValidationMessageFor(model => model.PersonnelID)
        </td>
     </tr>
</table>
...

I know enough about cascading updates with jquery. My Q is that is it possible to perform a cascade update to these DDLs without needing to write an iteration of <option>something</option> for the Detail DDL? If no, what's the handiest alternative?

note: In fact I'm trying to render the detail DDL with html helper because of the convention of model binding. If I have no choice but to render it by <select id=""></select>, how can I bind this select element to the model?

thanx

Update: It seems that there's no way... (still waiting and indeed searching...)


Solution

  • Yeah! of course there is: See this for details. However it needs an additional action method and a partial view for each Detail DDL.

    finally I like to decide to go through JQuery and iterationally adding <options> over the JsonResult...