title: asp.net mvc partial view redirect or show error
body: <p>I have a login widget that is a partial view that I want updated with model validation errors when there is a problem otherwise the page should be reloaded.</p>
  
  <p>I have the following</p>
  
  <p><strong>LogOn.cshtml</strong></p>
  
  <pre><code>@{
      var ajaxOpts = new AjaxOptions {OnSuccess = "success"};
  }
  
  &lt;div id="login"&gt;
      @Html.ValidationSummary(excludePropertyErrors: true)
  
      &lt;h2&gt;Start by Logging in&lt;/h2&gt;
  
      @using (Ajax.BeginForm("LogOn", "Account", ajaxOpts))
      {
          @Html.Hidden("returnUrl", Request.Url.PathAndQuery)
  
          &lt;table width="100%" border="0" cellspacing="0" cellpadding="5"&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;span class="bluey"&gt;Username:&lt;/span&gt;&lt;br /&gt;
                      @Html.TextBoxFor(m =&gt; m.UserName, new {tabindex = "1", Class = "field"})
                      @Html.ValidationMessageFor(m =&gt; m.UserName, "*")
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;span class="bluey"&gt;Password:&lt;/span&gt;&lt;br /&gt;
                      @Html.TextBoxFor(m =&gt; m.Password, new {tabindex = "2", Class = "field"})
                      @Html.ValidationMessageFor(m =&gt; m.Password, "*")
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;input name="login" type="submit" value="Submit" class="input_btn" tabindex="3" /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;@Html.CheckBoxFor(m =&gt; m.RememberMe) @Html.LabelFor(m =&gt; m.RememberMe) &lt;span class="bluey"&gt;&amp;nbsp; | &amp;nbsp;&lt;/span&gt; @Html.ActionLink("Forgot Password?", "Password", "User")&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
      }
  &lt;/div&gt;
  
  &lt;script&gt;
      function success(context) {
          //var returnUrl = context.get_data().returnUrl;
          //if (returnUrl) {
          //    window.location.href = returnUrl;
          //} else {
              $("#login").replaceWith(context);
          //}
  
      }
  
  &lt;/script&gt;
  </code></pre>
  
  <p>and the action that gets called is </p>
  
  <pre><code>    [HttpPost]
      public ActionResult LogOn(LogOnModel userDetails, string returnUrl)
      {
          if (ModelState.IsValid)
          {
              if (Membership.ValidateUser(userDetails.UserName, userDetails.Password))
              {
                  FormsAuthentication.SetAuthCookie(userDetails.UserName, userDetails.RememberMe);
  
                  return Redirect(returnUrl);
              }
              ModelState.AddModelError("", "The username or password provided was incorrect");
          }
  
          return PartialView(userDetails);
      }
  </code></pre>
  
  <p>I have it working so that when data is incorrect the partial view is redisplayed with errors however this part only works if I comment out the other lines of javascript in success(context) that are there for when the user logs in successfully and the page should be redirected.</p>
  
  <p>Is there a better way to do this?</p>
  
  <p>I did try returning anonymous objects with Json() that had a status property and either a returnUrl or Html however I could not figure out how to get the html that would have been generated by PartialView(userDetails) into the Json() call, and it seems like that might be the wrong way to go about things anyway.</p>