title: ASP .NET Core 5 Razor Pages: how to properly use Partial View and validate it's model state?
body: <p>I'm using ASP .NET Core 5 Razor Pages.</p>
  <p>My goal is to has a set of Partial Views (for reusability purpose) that I can use on several pages. Each Partial View has a form with its own custom post event handler (it will be processed by a code-behind of a pages that will contain this Partial View).</p>
  <p>N.B. Some pages can contain <strong>two or even more</strong> different Partial Views! And I need that Partial View models to be validated <strong>independently of each other</strong> (in two separate custom event handlers).</p>
  <p>Here is simplified code that I use for today. <strong>Partial View model</strong> (contains some data for a user):</p>
  <pre><code>public partial class User
  {
      [Required]
      public string Name { get; set; }
      [Required]
      public string Surname { get; set; }
  }    
  
  public class UserModel : PageModel
  {
      [BindProperty]
      public User user { get; set; }
      [TempData]
      public string StatusMessage { get; set; }
      public UserModel()
      {
          user = new User();
      }
  }
  </code></pre>
  <p><strong>_UserPartial.cshtml</strong> (displays that user data):</p>
  <pre><code>@model UserModel
  
  &lt;div class=&quot;row text-warning&quot;&gt;
      &lt;div class=&quot;col-md-4&quot;&gt;
          &lt;form method=&quot;post&quot; asp-page-handler=&quot;UserEdited&quot;&gt;
              &lt;div asp-validation-summary=&quot;ModelOnly&quot;&gt;&lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label asp-for=&quot;user.Surname&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;input asp-for=&quot;user.Surname&quot; class=&quot;form-control&quot; /&gt;
                  &lt;span asp-validation-for=&quot;user.Surname&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label asp-for=&quot;user.Name&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;input asp-for=&quot;user.Name&quot; class=&quot;form-control&quot; /&gt;
                  &lt;span asp-validation-for=&quot;user.Name&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;input type=&quot;submit&quot; value=&quot;Save user data&quot; /&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  <p><strong>Index.cshtml</strong> (main page that contains Partial View):</p>
  <pre><code>@page
  @model IndexModel
  @{
      ViewData[&quot;Title&quot;] = &quot;Main page&quot;;
  }    
  
  @if (!String.IsNullOrWhiteSpace(@Model.StatusMessage))
  {
      &lt;div class=&quot;text-center&quot;&gt;
          &lt;h4 class=&quot;text-warning&quot;&gt;@Model.StatusMessage&lt;/h4&gt;
      &lt;/div&gt;
  }    
  
  &lt;div class=&quot;text-center&quot; id=&quot;mainView&quot;&gt;
      &lt;p&gt;Some text in a main view&lt;/p&gt;
      &lt;p&gt;Some &lt;a href=&quot;https://docs.microsoft.com/aspnet/core&quot;&gt;link&lt;/a&gt; in a main view.&lt;/p&gt;
  &lt;/div&gt;
  
  &lt;div class=&quot;text-center&quot; id=&quot;userPartialView&quot;&gt;
      @{await Html.RenderPartialAsync(&quot;_UserPartial&quot;, IndexModel.userModel);}
  &lt;/div&gt;
  //Some other Partial View (which contains some data for a message)
  &lt;div class=&quot;text-center&quot; id=&quot;userPartialView&quot;&gt;
      @{await Html.RenderPartialAsync(&quot;_MessagePartial&quot;, IndexModel.messageModel);}
  &lt;/div&gt;
  </code></pre>
  <p><strong>Index.cshtml.cs</strong> (code-behind of a main page):</p>
  <pre><code>public class IndexModel : PageModel
  {
      public static UserModel userModel { get; set; }
      //A model for some other Partial View (which contains some data for a message)
      public static MessageModel messageModel { get; set; }
      [TempData]
      public string StatusMessage { get; set; }        
  
      public IActionResult OnGet()
      {
          userModel = new UserModel();
          messageModel = new MessageModel();
          return Page();
      }
  
      public IActionResult OnPostUserEdited()
      {
          if (!userModel.ModelState.IsValid)
          {
              return Page();
          }
  
          StatusMessage = &quot;User data was saved!&quot;;
          return RedirectToPage();
      }
  }
  </code></pre>
  <p>Problem is that <strong>userModel.ModelState</strong> is always <strong>valid</strong> even if <strong>Name</strong> and <strong>Surname</strong> are empty:
  <a href="https://i.stack.imgur.com/aaQ8I.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aaQ8I.png" alt="Looks like UserModel is not validaiting at all" /></a>
  Looks like <strong>UserModel</strong> is not validaiting at all.</p>
  <p>And I have a strong feeling that I'm using Partial Views tha wrong way (not the way they were supposed to be used).</p>
  <p>So what's wrong with my code? How to properly use Partial View and validate it's model state? Any help is appreciated.</p>