所以我的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 教程中的概念),然后将使用这个 List实体,显示一个字段,从另一个获取值(“名称”和“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 发送的。
请问有什么想法吗?