title: ASP.NET MVC Validation in Partial View and return to Parent view
body: <p><strong>My Background</strong></p>
  
  <p>I am working on first serious project using ASP.NET MVC 4. I am working on web development since classic ASP days and have got good hold on Webforms. MVC is very exciting and am doing good progress. But now I am in a situation where I need help on this forum.</p>
  
  <p><strong>Query background</strong></p>
  
  <p>I have a parent view and inside it there is a partial view. Partial view contains a form and submit button. The partial view has its own local view model and that view model is one of the properties of the Parent view model.</p>
  
  <p>In case the validations on partial views fail, I want to, show the parent view as it is and highlight invalid fields in partial view.</p>
  
  <p>The code is not breaking anywhere but when there is a validation error, somehow, i am not finding right way to show parent view with initialised model passed to it. And of course, to highlight the errors in partial view.</p>
  
  <p>Any help would be appreciated. Thanks.</p>
  
  <p>Code looks like following:</p>
  
  <p><strong>View Models:</strong></p>
  
  <pre><code>public class ParentViewModel
  {
      public int TitleId { get; set; }
      public string Name { get; set; }
      public ChildViewModel Child { get; set; }
  }
  
  public class ChildViewModel
  {
      [Required]
      public decimal Rating { get; set; }        
  
      [Required]
      [StringLength(500)]
      [Display(Description = "Review")]
      [RegularExpression("([a-zA-Z0-9 .&amp;'-]+)", ErrorMessage = "Enter only alphabets and numbers of First Name")]
      public string ReviewText { get; set; }
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public class TestController : Controller
  {
      public ActionResult Index()
      {
          var model = new ParentViewModel()
                          {
                              TitleId = 1,Name = "Parent name",
                              Child = new ChildViewModel()
                                          {
                                              Rating = 2.5M, ReviewText = "Its a must watch!"
                                          }
                          };
          return View("Index", model);  
      }
  
      [HttpPost]
      public ActionResult SubmitReview(ChildViewModel model)
      {
          if (ModelState.IsValid )
          {
              return View("_child", model);
          }
  
          ModelState.AddModelError("", "Some Error.");
          return View("_child", model);
      }
  }
  </code></pre>
  
  <p><strong>Parent View</strong></p>
  
  <pre><code>@model ParentViewModel
  @{
      Layout = null;
  }
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Index&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div&gt;
          @Model.TitleId, @Model.Name        
      &lt;/div&gt;
      &lt;div&gt;
          @Html.Partial("_child", Model.Child)
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>Partial View (_child.cshtml)</strong></p>
  
  <pre><code>@model ChildViewModel
  @using (Html.BeginForm("SubmitReview", "Test"))
  {
      @Html.ValidationSummary(true)
  
      @Html.TextBoxFor(m =&gt; m.Rating)     @Html.ValidationMessageFor(m =&gt; m.Rating)
      @Html.TextBoxFor(m =&gt; m.ReviewText) @Html.ValidationMessageFor(m =&gt; m.ReviewText)
      &lt;input type="submit" value="Log in" /&gt;
  }
  </code></pre>