0

I ran into a scenario where I have to programmatically submit an asp.net MVC form using JavaScript (form.Submit()) when the user clicks a button outside of the form tags.

I believe when this is the case, none of the built-in ASP.NET MVC functionality is applied. Please correct me if I am wrong.

  1. No AntiForgery token is validated (which should work with the Controller tag [ValidatAntiForgeryToken])
  2. Unobtrusive validation does not work

I am assuming that in this scenario there really isn't much of an option but to figure out another way to handle CSRF. Additionally, another way to handle client-side validation, not being able to validate based on model data-annotations.

8
  • 2
    The ValidatAntiForgeryToken will be processed sever-side if specified. It must be present in the posted payload, but form.serialize() will do that. Unobtrusive validation will be bypassed as it hooks into the form submit event Commented Feb 10, 2016 at 16:06
  • Alright, so your saying I don't have to worry about the ValidateAntiForgeryToken since it will be sent when using AJAX and data: $('#myForm').serialize()... But as far as unobtrusive validation along with the model I am basically stuck using jQuery Validation to manually validate everything, without relying on the ViewModel annotations. Correct? Commented Feb 10, 2016 at 16:11
  • @GoneCodingGoodbye The reason I am asking about this unobtrusive validation thing is because I am using a single form with a wizard approach using jQuery steps. Everytime the continue button is clicked that is not the actual submit button I would like to still use Unobtrusive validation to validate each step... I guess this isn't possible. I wonder why there is no way to manually call unobtrusive validation. Commented Feb 10, 2016 at 16:13
  • There is nothing stopping you call unobtrusive validation yourself (just been information I can no longer contribute to SO from work, so good luck with this). Commented Feb 10, 2016 at 16:13
  • @GoneCodingGoodbye Thanks for the info! I am yet to find any examples on something like that. Commented Feb 10, 2016 at 16:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.