【发布时间】:2012-10-09 18:12:59
【问题描述】:
所以我的Controller的代码如下:
private CommunityModelsContext dbCommunities = new CommunityModelsContext();
// GET: /Home/
public ActionResult Index()
{
//retrieve the Communities
ViewBag.Communities = dbCommunities.Communities.ToList();
return View();
}
而我的视图有这条重要的线来启动部分视图
<div id="LeftView" class="PartialView">@{Html.RenderPartial("CommunitiesPartial");}</div>
在部分视图中,我正在尝试创建一个 DropDownList(我还在学习,这是一个练习应用程序,只是为了看看我是否理解了 asp.net 教程中的概念),然后取这个实体列表,显示一个字段,从另一个(“名称”和“id”)获取值
@model BuildingManagement.Models.Community.Community
@Html.BeginForm("Index","CommunityController")
{
<div>
@Html.LabelFor(x => x.Name)
@Html.DropDownList("Community" , new SelectList(Model.Name,"id","Name"))
</div>
}
现在这会引发 NullReference 异常,模型为空。 Index 页面中没有模型,也没有绑定任何东西,但是数据是通过 ViewBag 发送的。
请给点意见?
【问题讨论】:
标签: asp.net-mvc-4 partial-views viewbag