[英]MongoDB referencing ASP.NET MVC
我的Web应用程序有问题。 在我的应用程序中,我有两个集合:“ post”和“ category”。 我想通过引用类别添加到帖子。 在帖子集合中,我具有字段“ MongoDBRef类别”,在帖子控制器中:
public ActionResult Create(Post post)
{
_category.GetCategory();
if (ModelState.IsValid)
{
post.Url = post.Title.GenerateSlug();
post.Author = User.Identity.Name;
post.Date = DateTime.Now;
post.Category = new MongoDBRef("Category", CategoryId);
_postService.Create(post);
return RedirectToAction("Index");
}
return View();
}
但是没有用。 谁能解决这个问题?
这是我在邮政课上的领域:
public MongoDBRef Category { get; set; }
和类别类:
public class Category
{
[ScaffoldColumn(false)]
[BsonId]
public ObjectId CategoryId { get; set; }
[ScaffoldColumn(true)]
[Required]
public string Name { get; set; }
}
我想创建具有某些类别的帖子,这是类别集合的参考,但是我不知道要这样做吗...
改变这个
post.Category = new MongoDBRef("Category", CategoryId);
至
post.Category = new MongoDBRef("Category", _category.CategoryId);
也同意评论者的意见-您不应在此处使用MongoDBRef。 它始终是一个类别,因此只需将类别ID放在字符串中即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.