title: Bootstrap modal dialog return message in MVC
body: <p>I need some help on return success or error message in bootstrap modal dialog from Partial View in mvc. </p>
  
  <p>When click the link in _LayoutAdmin view, partial view Impersonation.cshtml will load into modal dialog. </p>
  
  <p>After input and click submit button, the popup box will close and post in controller. How to show the error message in modal dialog if user input data is not existed?</p>
  
  <p>Any guidance is highly appreciate!</p>
  
  <p>Screenshot:</p>
  
  <p><a href="https://i.stack.imgur.com/hWNhN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hWNhN.png" alt="enter image description here"></a></p>
  
  <p>_LayoutAdmin.cshtml:</p>
  
  <pre><code>&lt;a href="@Url.Action("Impersonation", "UserRoles")" class="modal-link"&gt;
    Impersonation &amp;nbsp;
     &lt;span class="glyphicon glyphicon-user" aria-hidden="true"&gt;&lt;/span&gt;
  &lt;/a&gt;
  
  &lt;div id="modal-container" class="modal fade" tabindex="-1" role="dialog"&gt;
  &lt;/div&gt;
  
  &lt;script&gt;
  
   $(function () {
  
                  $('body').on('click', '.modal-link', function (e) {
                      e.preventDefault();
                      $(this).attr('data-target', '#modal-container');
                      $(this).attr('data-toggle', 'modal');
                  });
                  // Attach listener to .modal-close-btn's so that when the button is pressed the modal dialog disappears
                  $('body').on('click', '.modal-close-btn', function () {
                      $('#modal-container').modal('hide');
                  });
                  //clear modal cache, so that new content can be loaded
                  $('#modal-container').on('hidden.bs.modal', function () {
                      $(this).removeData('bs.modal');
                  });
                  $('#CancelModal').on('click', function () {
                      return false;
                  });
          });
  
      &lt;/script&gt;
  </code></pre>
  
  <p>(Partial View) Impersonation.csthml:</p>
  
  <pre><code>@model FAB_Portal.Models.FAB_View
  @using (Html.BeginForm())
  { 
      &lt;script type="text/javascript"&gt;
  
          $("a#impersonate").click(function () {
              var username = $('#UserName').val();
              $.ajax({
                  type: "POST",
                  url: "@Url.Action("Impersonation", "UserRoles")",
                  data: { UserName: username },
                  success: function (result) {
                      if (result.success) {
                          $('#modal-dialog').modal('hide');
  
                      } else {
                          $('#modal-body').html(result);
                      }
                  }
              });
          });
  
      &lt;/script&gt;
  
  &lt;div class="modal-dialog"&gt;
      &lt;div class="modal-content"&gt;
  
          &lt;div class="modal-header"&gt;
              &lt;button type="button" class="close" data-dismiss="modal" aria-hidden="true"&gt;&amp;times;&lt;/button&gt;
              &lt;h4 class="modal-title" id="myModalLabel"&gt;&lt;span class="glyphicon glyphicon-user" aria-hidden="true"&gt;&lt;/span&gt;&amp;nbsp;Impersonation&lt;/h4&gt;
          &lt;/div&gt;
  
          &lt;div class="modal-body"&gt;
              &lt;div class="form-horizontal"&gt;
                  @Html.Label("Impersonate", htmlAttributes: new { @class = "control-label col-md-4" })
                  @Html.TextBox("UserName", null, htmlAttributes: new { @class = "form-control" })
                  &lt;text class="text-danger"&gt;@ViewBag.Error&lt;/text&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="modal-footer"&gt;
              @using (Html.BeginForm("Impersonation", "UserRoles", FormMethod.Post))
              {
                  &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Cancel&lt;/button&gt;
                  &lt;a class="btn btn-danger btn-ok" id="impersonate" &gt;Submit&lt;/a&gt;
              }
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  }
  </code></pre>
  
  <p>UserRoles.Controller:</p>
  
  <pre><code>public ActionResult Impersonation()
  {
      return PartialView("Impersonation");
  }
  
  [HttpPost]
  public ActionResult Impersonation(FAB_View model)
  {
      if (ModelState.IsValid)
      {
          UserRoleHelper userrolehelper = new UserRoleHelper();
          bool validuser = userrolehelper.CheckValidUser(model.UserName);
          if (validuser == false)
          {
              ViewBag.Error = "Don't have such user!";
              return PartialView("Impersonation");
          }
          userrolehelper.StartImpersonate(model.UserName);
      }        
      return PartialView("Impersonation");
  }
  </code></pre>