我正在尝试使用 MVC 制作一个简单的论坛,但我无法弄清楚为什么发布回复的用户会被重复。
这是回复操作:
[HttpPost]
public ActionResult Reply(string Title, string Content,int ReplyTo)
{
Post masterPost = db.Posts.FirstOrDefault(p => p.PostID == ReplyTo);
Post post = new Post();
post.PostID = 0;
post.CreatedOn = DateTime.Now;
post.ModifiedOn = DateTime.Now;
post.ReplyTo = masterPost;
post.Forum = db.Forums.FirstOrDefault(f=>f.ForumID == masterPost.Forum.ForumID);
post.User = (User)Session["User"];
post.Title = Title;
post.Content = Content;
//if (ModelState.IsValid)
//{
db.Posts.Add(post);
db.SaveChanges();
return RedirectToAction("View", "Posts", new { id = ReplyTo });
//}
return View(post);
}
这是 Post 实体:
public class Post
{
[Key]
public int PostID { get; set; }
public string Title { get; set; }
[DataType(DataType.MultilineText)]
public string Content { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime? ModifiedOn { get; set; }
public virtual Forum Forum { get; set; }
public virtual User User { get; set; }
public virtual Post ReplyTo { get; set; }
}
这是用户实体:
public class User
{
public int UserID { get; set; }
public string Name { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public DateTime RegisteredOn { get; set; }
}
每当调用ReplyTo 操作时,它都会创建帖子,但它也会复制存储在会话中的用户(具有不同的用户ID)。
我究竟做错了什么?