我正在尝试弄清楚如何在创建新帖子时插入标签对象列表。
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public ICollection<PostTag> PostTags { get; set; }
}
public class PostTag
{
public int TagId { get; set; }
public Tag Tag { get; set; }
public int PostId { get; set; }
public Post Post { get; set;}
}
public class Tag
{
public int TagId { get; set; }
public string Text { get; set; }
public ICollection<PostTag> PostTags { get; set }
// PostController
Content = model.Content,我无法弄清楚如何获取标签我是否使用Tags,PostTags?我很困惑。
public IActionResult Add(NewPostModel model)
{
return new Post()
{
Title = model.Title,
Tags = ??
}
}
答案 0 :(得分:1)
使用对象初始化程序语法:
public IActionResult Add(NewPostModel model)
{
return new Post()
{
Title = model.Title,
PostTags = new List<PostTag> ()
{
new PostTag ()
{
Tag = new Tag ()
},
new PostTag ()
{
Tag = new Tag()
}
}
}
}
您可以找到更多信息here