Imagine following situation: I want to let the user make a withdrawal. If it succeeds I should redirect him to action called Index
of HomeController
. If not (for instance, the balance of the account is less than the withdrawal sum), I want to display validation based error.
Now, I can't specify appriopriate validation rule while in the project phase (since it's not static rule like Required
, the balance changes all the time), so I'm forced to make some runtime checking on user submit. That's okay, but now I've started wondering if in this case this is possible not to post back the page after sumbit.
Therefore, I've tried @using(Ajax.BeginForm(...){}
and in the controller's appriopriate I could verify the user input. If this is incorrect, I could add the error to the model (general, or to some specified property) and return partial view including this error, not causing page post-back. This makes the same behavior as standard jQuery validation, yet of course it does not really stops post-back and just shows updated partial view.
The problem is if the input is correct- then using RedirectToAction("Index", "Home")
makes generating the Index.cshtml
as partial view too (so header and footer of my layout are doubled in this case) and that's understandable since it's stil AJAX request.
Now, my question is- first of all, is my considerations needed at all? I mean, I just thought not posting-back in this case could be more user-friendly. And if so, is there any other approach I could use in this case? I need fetching data from the given context (like database), then I can't just stop the postback on the client side.