Asp.net mvc 在视图中访问viewbag
net MVC。我已经为viewbag属性指定了一个对象列表,有人能告诉我如何在视图中获取该列表,以便在下拉列表中使用它吗?这是我的控制器代码和视图代码 控制器:Asp.net mvc 在视图中访问viewbag,asp.net-mvc,razor,Asp.net Mvc,Razor,net MVC。我已经为viewbag属性指定了一个对象列表,有人能告诉我如何在视图中获取该列表,以便在下拉列表中使用它吗?这是我的控制器代码和视图代码 控制器: public ActionResult GetSection(int sectionId,int contactId) { ContactDetailSectionModel contactDetailSection = new ContactDetailSectionModel { Sec
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)
我认为
@Html.DropDownlist
中的第一个参数应该是字符串或一些标量,它不能是集合。不,我知道了。我将viewbags属性强制转换为列表。谢谢你的帮助