我試圖添加一個表單以允許用戶評論我的博客應用程序上的帖子。到目前爲止,我已向表單添加了帖子詳細信息視圖,並且我可以提交註釋,並將它們正確添加到我的數據庫中。但是,我向用戶顯示驗證錯誤時遇到問題。評論表單包含在部分視圖中,並在帖子詳細信息視圖中使用Html.RenderAction
進行呈現。我想強調的是,我不想爲此使用AJAX,因爲我希望從逐步增強的角度來解決這個問題。ASP.NET MVC3:從父視圖中的子視圖顯示驗證錯誤
這裏的相關帖子行動:
[HttpPost, Authorize]
public ActionResult AddComment(CommentViewModel newComment)
{
if (ModelState.IsValid)
{
Comment comment = new Comment(_userRepository.GetByUsername(User.Identity.Name));
Mapper.Map(newComment, comment);
_commentRepository.Add(comment);
_postsRepository.CommentAdded(comment.Article);
return RedirectToAction("Index", new { id = newComment.PostID });
}
// What do I do here?
}
我已經試過這裏返回意見的幾種方法,但我的問題是更加複雜的,我在父行動正在進行一些控制器參數驗證:
//
// GET: /Posts/5/this-is-a-slug
public ActionResult Index(int id, string slug)
{
PostViewModel viewModel = new PostViewModel();
var model = _postsRepository.GetByID(id);
if (model != null)
{
if (slug == null || slug.CompareTo(model.Slug) != 0)
{
return RedirectToActionPermanent("Index", new { id, slug = model.Slug });
}
else
{
_postsRepository.PostVisited(model);
Mapper.Map(model, viewModel);
viewModel.AuthorName = _userRepository.GetById(model.AuthorID);
}
}
return View(viewModel);
}
此操作基本上模仿SO的URL如何工作。如果提供了帖子ID,則帖子從數據庫中取出,同時創建帖子時創建塊。如果URL中的slu does與數據庫中的slu match不匹配,它將重定向爲包含slu。。這是工作很好,但它確實意味着我在想填充我父視圖模型,這是問題如下:
public class PostViewModel
{
public int PostID { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public string Slug { get; set; }
public DateTime DatePublished { get; set; }
public int NumberOfComments { get; set; }
public int AuthorID { get; set; }
public string AuthorName { get; set; }
public List<CommentViewModel> Comments { get; set; }
public CommentViewModel NewComment { get; set; }
}
我希望會的工作是填充PostViewModel.NewComment
,測試以查看它是否有數據然後用它來顯示任何模型錯誤。不幸的是,我失去了如何實現這一點。 This question幫助我塑造了我的方法,但它並沒有完全回答我的問題。
有人能給我一個正確的方向輕輕推?如果我的方法看起來不合理,我想知道爲什麼以及可能的解決方案是什麼。
非常感謝提前。