2013-02-19 187 views
0

net MVC。我已经为viewbag属性分配了一个对象列表,任何人都可以告诉我如何在视图中获取列表以在下拉列表中使用它?这里是我的控制器代码&视图代码在视图中访问viewbag

控制器:

public ActionResult GetSection(int sectionId,int contactId) 
     { 
      ContactDetailSectionModel contactDetailSection = new ContactDetailSectionModel { SectionId = sectionId,ContactId=contactId }; 
      contactDetailSection.FetchAllSubsections(); 

      ContactDetailSectionModel customSections = new ContactDetailSectionModel(); 
      customSections.FetchCustomSubSections(); 

      if(customSections != null && customSections.ContactDetailSubSections != null) 
      { 
       ViewBag.CustomSubSections = customSections.ContactDetailSubSections; 
      } 

      return PartialView("~/Views/Contacts/Details/EditSection.cshtml", contactDetailSection); 
     } 

查看代码:

@Html.DropDownListFor(m => m.ContactDetailSubSections[1], new SelectList(ViewBag.CustomSubSections , "Name", "Name",Model.ContactDetailSubSections[1].Name)) 
@Html.TextAreaFor(m => m.ContactDetailSubSections[1].Text) 

回答

0

我想在你的@Html.DropDownlist应该是字符串的第一个参数或某些标量不能是一个采集。

+0

不,我知道了。我将viewbags属性放入列表中。谢谢您的帮助 – 2013-02-19 09:57:58