title: Razor Communicate Display of Modal from PageModel's OnPost() Method
body: <p>I want to communicate from my Razor PageModel's OnPost() method to display the modal upon validation errors for it. Which basically means changing the modal's css from display none to block. Is there a way for this to be done?</p>
  <p>Currently on return Page() the modal is hidden because thats what its css is initially set to, and is normally displayed on the user clicking the button to show it. I marked in my PageModel code where Id like the communication to occur</p>
  <pre><code>@page
  @{
      ViewData[&quot;Folder&quot;] = &quot;CourtUser&quot;;
      &lt;form asp-action=&quot;AddorEditUsersOnHearing&quot; method=&quot;post&quot; name=&quot;AddorEditUsersOnHearingForm&quot; id=&quot;AddorEditUsersOnHearing&quot;&gt;
  
          &lt;div class=&quot;container&quot;&gt;
              &lt;div class=&quot;row&quot;&gt;
                  &lt;div class=&quot;col-md-8 mb-4&quot;&gt;
                      &lt;p&gt;Add/Edit Users on  &lt;b style=&quot;font-style:italic&quot;&gt;@Model.HearingName &lt;/b&gt;&lt;/p&gt;                   
                      &lt;div class=&quot;modal&quot; tabindex=&quot;-1&quot; role=&quot;dialog&quot; id=&quot;AddUserForm&quot;&gt;
                          &lt;div class=&quot;modal-dialog&quot; role=&quot;document&quot;&gt;
                              &lt;div class=&quot;modal-content&quot;&gt;
                                  &lt;div class=&quot;modal-header&quot;&gt;
                                      &lt;h5 class=&quot;modal-title&quot;&gt;Add User&lt;/h5&gt;
                                      &lt;button type=&quot;button&quot; onclick=&quot;closeAddUserForm()&quot; class=&quot;close&quot; data-dismiss=&quot;modal&quot; aria-label=&quot;Close&quot;&gt;
                                          &lt;span aria-hidden=&quot;true&quot;&gt;&amp;times;&lt;/span&gt;
                                      &lt;/button&gt;
                                  &lt;/div&gt;
                                  &lt;div class=&quot;form-group&quot; style=&quot;margin-top:5px;padding-left:45px&quot;&gt;
                                      &lt;label asp-for=&quot;AddUserInput&quot; style=&quot;width:100px&quot;&gt;&lt;/label&gt;
                                      &lt;input asp-for=&quot;AddUserInput&quot; class=&quot;form-control col-4&quot; id=&quot;EmailInputBox&quot; style=&quot;display:inline-block&quot; onchange=&quot;ValidateEmail()&quot; /&gt;
                                      &lt;span style=&quot;display:block&quot; asp-validation-for=&quot;AddUserInput&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
  
                                  &lt;/div&gt;
                                  &lt;div class=&quot;modal-footer&quot;&gt;
                                      &lt;button class=&quot;btn btn-primary&quot; style=&quot;margin:0 auto&quot; asp-page-handler=&quot;AddUser&quot; name=&quot;AddUserSubmit&quot; value=&quot;Yes&quot;&gt;Submit&lt;/button&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;input asp-for=&quot;HearingId&quot; type=&quot;hidden&quot; /&gt;
                      &lt;input asp-for=&quot;HearingName&quot; type=&quot;hidden&quot; /&gt;
  
                      &lt;button type=&quot;button&quot; class=&quot;btn btn-primary&quot; onclick=&quot;ShowAddUserForm()&quot;&gt;Add User&lt;/button&gt;
                      &lt;button style=&quot;float:right&quot; class=&quot;btn btn-primary&quot;&gt;Remove User(s)&lt;/button&gt;
  
  
                  &lt;/div&gt;
              &lt;/div&gt;
  
  
          &lt;/div&gt;
      &lt;/form&gt;
  
  }
  
  &lt;script type=&quot;text/javascript&quot;&gt;
      function ShowAddUserForm() {
          document.getElementById(&quot;AddUserForm&quot;).style.display = &quot;block&quot;;
      }
      function closeAddUserForm() {
          document.getElementById(&quot;AddUserForm&quot;).style.display = &quot;none&quot;;
      }
  &lt;/script&gt;
  </code></pre>
  <pre><code>        public IActionResult OnPostAddUser()
          {
              if (ModelState.IsValid)
              {
                  if (AddUserInput == null)
                  {
                     
                      ModelState.AddModelError(&quot;AddUserInput&quot;, &quot;Please enter an email&quot;);
                       
                      UsersonHearingList = HttpContext.Session.GetObjectFromJson&lt;List&lt;UsersModel&gt;&gt;(&quot;UsersonHearingList&quot;);
                       
  //*****This is where I want to communicate to the view to display the modal.*******
                      return Page();
                  }
  
                 
              }
              else
              {
                  return RedirectToPage(&quot;/Shared/Error&quot;);
              }
          }
  </code></pre>