这是我的组合模型:
public class AddArticleModel
{
public TBL_ARTICLES Article { get; set; }
public IEnumerable<TBL_CATEGORIES> Categories { get; set; }
}
以及使用模型的控制器。
public ActionResult AddArticle()
{
AddArticleModel AddArticleModel = new AddArticleModel();
AddArticleModel.Categories = entity.TBL_CATEGORIES.Select(a => a);
return View(AddArticleModel);
}
并查看:
@model DunyaYazilim.Models.AddArticleModel
@{
ViewBag.Title = "AddArticle";
}
@using (Html.BeginForm((string)ViewBag.FormAction, "Home"))
{
<fieldset>
<legend>Add Article Form</legend>
<ol>
<li>
@Html.DropDownListFor(m => m.Categories, Model.Categories.Select(c => new SelectListItem { Text = c.Name, Value = c.CategoryID.ToString() }), "-----Select Category----")
</li>
</ol>
<input type="submit" value="Send" />
</fieldset>
}
并在控制器中发布方法:
[HttpPost]
public ActionResult AddArticle(AddArticleModel AddArticleModel)
{
//Insert operations:
return View(AddArticleModel);
}
我的问题:当我发布表单时,出现错误:对象引用未设置为对象的实例。排队:13
Line 12: <li>
Line 13: @Html.DropDownListFor(m => m.Categories, Model.Categories.Select(c => new SelectListItem { Text = c.Name, Value = c.CategoryID.ToString() }), "-----Select Category----")
Line 14: </li>
Line 15: <li>
这是什么原因?
注意:这个网站上有很多例子。我尝试了很多,但我找不到错误的原因。
谢谢。