0
所以我的控制器的代碼如下:從ViewBag傳遞一個值的局部視圖
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異常,模型爲null。索引頁面中沒有模型,但它不受任何約束,但是,數據通過ViewBag發送。
想法請?