0

我看到this,但我有不同的问:我有这样一个观点:级联两个@ Html.DropDownListFor的更新中MVC4与模型结合

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

我有足够的了解与jquery级联更新。我的问题是,是否可以对这些DDL执行级联更新,而无需为Detail DDL编写<option>something</option>迭代?如果不是,最好的选择是什么?

注:其实我试图渲染,因为模型的结合公约的HTML辅助细节DDL。如果我别无选择,只能将其渲染<select id=""></select>,那么如何将此select元素绑定到模型?

感谢名单

更新:看来,有没有办法......(仍在等待确实搜索...)

回答

0

呀!当然有:See this for details。但是,它需要每个详细DDL的附加操作方法和局部视图。

最后我想决定要经过jQuery和iterationally加入<options>在JsonResult ...

0

来填充MVC一个下拉列表使用这个作为你的视图替代:

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

支持查看你应该写在相应的控制措施如下:

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

为了填补ViewData的,你需要一个受文者在同一个控制器,如下所示蓄水功能:

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的是一个通用的清单,类型强制转换为Html.Dropdownlistfor IEnumerable的项目列表项。