开发者

display validation of child in parent view asp.net mvc 3

Hi every body I am not professional in mvc3 , i have a problem for validation of child in parent view i have a page for describing product ( called Details ) and have a partial view for commentting on specific product ( named addUserReview ) , i use this partial view in Details view , when a user click on send button for submiting a comment if input data that user entered has problem the result displayed on addUserReviwe page . but i want to errors display in my Details page , here is my code :

 public ActionResult AddUserReview(FormCollection form )
    {

        UserReview userReview = new UserReview();
        if(ModelState.IsValid)
        {

            TryUpdateModel(userReview);
            // check whether user is Login or not
            userReview.UserName = User.Identity.IsAuthenticated ? User.Identity.Name : Constants.AnonymousUserReviewerName;
            storeDB.UserReviews.Add(userReview);
            try{
                storeDB.SaveChanges();
            }
            catch(Exception ex )
            {

                return View();

     开发者_运维知识库       }
        }
        return View();
    }


Because of you post formcollection to action you should check form properties manually.

ViewData.ModelState.AddModelError("XXX", form["XXX"], "XXX is wrong");

in View you can show error:

<%= Html.ValidationMessage("XXX") %>

But I recommend to make model and pass model to action. Not formCollection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜