I am sorry if this has been asked already, but I have been looking for sometime but all I have found are rather old posts (mvc1, mvc2). I have a form which I would like to submit via Ajax.
This looks like it would work but does not cover server side validation.
1) I am unsure if I should use the AjaxHelper.BeginForm
or use raw jquery calls ($.ajax)
? What is the recommended approach here?
2) How do I handle client and server side validation? I am hoping the mvc framework provides a built in mechanism for dealing with this?
There are some validations which I am only doing server side. Would using a ValidationSummary
still work here?
I am using asp.net mvc3/razor with unobtrussive javascript validation.
Thank you!
Edit: (as requested by Bobby B below). This was added months after asking the question as a user wanted to know how to use AjaxHelper
This is the javascript code I used:
<script type="text/javascript">
function ajaxValidate() {
return $('form').validate().form();
}
function getGbPostSuccess(ajaxContext){
// .... it is not necessary to do anything here.
}
function showFaliure(ajaxContext){
// handle failure
}
HTML snippet:
@using (Ajax.BeginForm("Index", "Home", new AjaxOptions
{
UpdateTargetId = "form1",
InsertionMode = InsertionMode.Replace,
OnBegin = "ajaxValidate",
OnSuccess = "getGbPostSuccess",
OnFailure = "showFaliure"
}))
{