Question

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...)

Was it helpful?

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...

OTHER TIPS

To populate a dropdownlist in MVC use this as an alternative in your View:

@Html.DropDownListFor(model => model.CourseID, new SelectList((IList<SelectListItem>)ViewData["MyCourse"],
                                            "Value", "Text"), new { @class = "span5" })

Supporting the View you should write the following in the corresponding controller action:

public ActionResult RoleList(int id)
{
     ViewData["MyCourse"] = FillCourseList(id);
     CourseModel model = new CourseModel();
     model.courseid= id;
     return View(model);
}

To fill ViewData you need a corresponding function in the same controller which is as follows:

public IList<SelectListItem> FillCourseList(int id)
        {
            List<master_tasks> lst = new List<master_tasks>();
            lst = _taskInterface.getMasterTasks();
            IList<SelectListItem> items = new List<SelectListItem>();

            items.Add(new SelectListItem
                {
                    Text = "Select Task",
                    Value = "0"
                });
            for (int i = 0; i < lst.Count; i++)
            {
                items.Add(new SelectListItem
                {
                    Text = lst[i].code + " - " + lst[i].name,
                    Value = lst[i].id.ToString()
                });
            }
            return items;
        }

IList is a generic list with list item typecasted for Html.Dropdownlistfor IEnumerable items.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top