Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC2 AJAX - determining UpdateTargetId based on the returned data
    text
    copied!<p><strong>The scenario:</strong> </p> <p>I'm creating a login form for an MVC2 application.</p> <p><strong>How i'm doing it</strong>:</p> <p>The form submits to an MVC2 action which validates the username/password. If it fails validation the action returns the form (a partial view) for the user to try again. If it passes validation the action returns the page the user was visiting before they logged in (a view).</p> <p><strong>What i want to happen:</strong></p> <p>1 - when the form is submitted and the user validates successfully, The returned result should replace the current page (like what happens if you don't set an UpdateTargetId).</p> <p>2 - When the form is submitted and the user fails validation, the returned result should replace the form (like what happens if you set the UpdateTargetID to the form's containing element).</p> <p><strong>The problem:</strong></p> <p>I can make both of those things work, but not at the same time. I can either have it always replace the current page, or always just replace the contents of the UpdateTargetId element. But I need it to be able to do either depending on whether the user successfully validated or not.</p> <p><strong>What I need</strong></p> <p>The ideal solution would be to be able to examine the result of the ajax request and determine whether to use the UpdateTargetId (replacing just the form) or not (replacing the whole page). I expect it would involve some work with jquery (assuming it's possible) but i'm not really that great with jquery yet to figure out how to do it myself. If it can't be done this way I'm also open to other methods/solutions for making it work in a similar fashion.</p> <p>Thanks in advance ..</p> <p>As requested. Code for the action. But as i said. It's the standard LogOn action that ships default with every mvc application. The only difference is it returns a partial view when validation fails.</p> <pre><code>[HttpPost] [MedLife.Code.Compression.CompressFilter] public ActionResult SignIn(LogOnModel model, string returnUrl) { if (ModelState.IsValid) { if (MembershipService.ValidateUser(model.UserName, model.Password)) { FormsService.SignIn(model.UserName, model.RememberMe); if (!String.IsNullOrEmpty(returnUrl)) { return Redirect(returnUrl); } else { return RedirectToAction("Index", "Home"); } } else { ModelState.AddModelError("", "The user name or password provided is incorrect."); } } // If we got this far, something failed, redisplay form return PartialView("Overlays/LoginControl", model); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload