【发布时间】:2015-09-30 23:45:17
【问题描述】:
我有这样的事情
[HttpPost]
public ActionResult CreatePost(Post post)
{
var categoryList = Request["CategoryList"].Split(',');
//I want to set my Category Name in here
foreach (var category in categoryList)
{
post.Categories.Add(new Category { Name = category });
}
post.CreatedDate = DateTime.Now;
post.CreatedBy = _userService.GetCurrentUser().Id;
_postService.CreatePost(post);
return View();
}
我想在我的帖子模型中设置public virtual ICollection<Category> Categories { get; set; }this。它有两个属性Id 和Name。我想在上面的CreatePost 方法中设置我的属性。我该怎么做?
【问题讨论】:
-
只是看不出问题出在哪里?
-
对象引用未设置为对象的实例。我得到了这个错误它通过添加这个来解决。类别 = 新列表();但是为什么要创建新实例呢?
标签: asp.net-mvc linq entity-framework-4