访问视图中的viewbag

时间:2013-02-19 06:26:40

标签: asp.net-mvc razor

净MVC。我已经为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)   

1 个答案:

答案 0 :(得分:0)

我认为@Html.DropDownlist中的第一个参数应该是字符串或某些标量,它不能是一个集合。