所以我有一个名为index的视图,它列出了我数据库中的所有线程.然后在该视图中我加载了线程上的所有注释.当我打电话给我应该创建新评论的表单时,它会一直告诉我模型状态无效.它告诉我它无法从类型字符串转换为类型配置文件或注释或标记.最初我把它作为我的代码:
public ActionResult AddComment(Thread thread, string commentBody)
{
if (ModelState.IsValid)
{
_repository.AddComment(thread, comment);
TempData["Message"] = "Your comment was added.";
return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)
然后我改为:
public ActionResult AddComment(Thread thread, string commentBody)
{
Profile profile = _profileRepository.Profiles.FirstOrDefault(x => x.Id == thread.ProfileId);
Tag tag = _tagRepository.Tags.FirstOrDefault(t => t.Id == thread.TagId);
thread.ThreadTag = tag;
thread.Profile = profile;
Comment comment = new Comment()
{
CommentBody = commentBody,
ParentThread = thread
};
if (ModelState.IsValid)
{
_repository.AddComment(thread, comment);
TempData["Message"] = "Your comment was added.";
return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)
这仍然告诉我模型状态无效.如何获得它以便它不会尝试改变状态? …