title: Returning both Partial View and a Message
body: <p>I have a partialview where it's possible to change a Connection String. When submitting an <code>Edit</code> action is called. From here I want to either return and re-open the partial view if I want the user to have a second go. If everything went well (or crashing) I want to call my JavaScript function <code>Logout</code>, that logs the user out and redirect to some startpage.</p>
  
  <p>Both solutions works, just not together. I'm clearly missing some best practice, what should I do?</p>
  
  <p><strong>Partial View: EditSetting</strong></p>
  
  <pre><code>@model WebConsole.ViewModels.Setting.SettingViewModel
  
  @using (Ajax.BeginForm("Edit", "Setting", new AjaxOptions { UpdateTargetId = "div" }, new { id = "editform" }))
  {
      &lt;fieldset&gt;
          @Html.AntiForgeryToken()
  
          &lt;div class="form-horizontal"&gt;
              @Html.ValidationSummary(true, "", new {@class = "text-danger"})
  
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.User, htmlAttributes: new {@class = "control-label col-md-2"})
                  &lt;div class="col-md-10"&gt;
                      @Html.EditorFor(model =&gt; model.User, new {htmlAttributes = new {@class = "form-control"}})
                      @Html.ValidationMessageFor(model =&gt; model.User, "", new {@class = "text-danger"})
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.Password, htmlAttributes: new {@class = "control-label col-md-2"})
                  &lt;div class="col-md-10"&gt;
                      &lt;input type="password" name="Password" id="Password" value=""/&gt;
                      @Html.ValidationMessageFor(model =&gt; model.Password, "", new {@class = "text-danger"})
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.DataSource, htmlAttributes: new {@class = "control-label col-md-2"})
                  &lt;div class="col-md-10"&gt;
                      @Html.EditorFor(model =&gt; model.DataSource, new {htmlAttributes = new {@class = "form-control"}})
                      @Html.ValidationMessageFor(model =&gt; model.DataSource, "", new {@class = "text-danger"})
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.InitialCatalog, htmlAttributes: new {@class = "control-label col-md-2"})
                  &lt;div class="col-md-10"&gt;
                      @Html.EditorFor(model =&gt; model.InitialCatalog, new {htmlAttributes = new {@class = "form-control"}})
                      @Html.ValidationMessageFor(model =&gt; model.InitialCatalog, "", new {@class = "text-danger"})
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/fieldset&gt;
  }
  </code></pre>
  
  <p><strong>JavaScript: Submit</strong></p>
  
  <pre><code>$('form').submit(function () {
      var $self = $(this);
  
      if ($(this).valid()) {
  
          // Change Connection String
          $.ajax({
              url: this.action,
              type: this.method,
              data: $(this).serialize(),
              success: function (message) {
  
                  // Use Partial View
                  //$('#myModal .modal-body').html(message);
  
                  // Conn Str is now changed. Log out and redirect
                  logOut($self, message);
              },
              error: function (message) {
                  logOut($self, message);
              }
          });
      }
      return false;
  });
  </code></pre>
  
  <p><strong>Action: Edit</strong></p>
  
  <pre><code>[HttpPost]
  public ActionResult Edit(SettingViewModel model)
  {
      // Validate inputs
      if (!ModelState.IsValid)
      {
          ModelState.AddModelError("", @"Not all inputs are valid.");
          return PartialView("EditSetting", model);
      }
  
      var sql = new DAL.SQL(DAL.SQL.GenerateConnectionString(model.DataSource, model.InitialCatalog, model.User, SecurePassword(model.Password)));
  
      // Validate Connection String
      if (!sql.Open())
      {
          ModelState.AddModelError("", @"Error. Unable to open connection to Database.");
          return PartialView("EditSetting", model);
      }
  
      // Validate a transaction
      if (!sql.IsRunningTransact())
      {
          ModelState.AddModelError("", @"Error. Unable to connect to Database Server.");
          return PartialView("EditSetting", model);
      }
  
      // Save Connection String
      BuildAndEncryptConnString(model);
  
      return Content("The Connection String is changed. Log in again to continue.");
  }
  </code></pre>