MVC2 AJAX - determining UpdateTargetId based on the returned data

Posted by DanielJW on Stack Overflow See other posts from Stack Overflow or by DanielJW
Published on 2011-01-06T12:29:18Z Indexed on 2011/01/06 12:54 UTC
Read the original article Hit count: 142

Filed under:
|
|
|

The scenario:

I'm creating a login form for an MVC2 application.

How i'm doing it:

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).

What i want to happen:

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).

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).

The problem:

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.

What I need

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.

Thanks in advance ..

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about asp.net-mvc