Skip to content

Instantly share code, notes, and snippets.

@casper-rasmussen
Created April 3, 2016 16:46
Show Gist options
  • Save casper-rasmussen/68fc4aae8fca9e68ab7ab5b33b0dfd99 to your computer and use it in GitHub Desktop.
Save casper-rasmussen/68fc4aae8fca9e68ab7ab5b33b0dfd99 to your computer and use it in GitHub Desktop.
public static class AjaxHelperExtensions
{
private static string GetLanguage()
{
if (HttpContext.Current == null || HttpContext.Current.Request.RequestContext == null)
return null;
return RequestContextExtension.GetLanguage(HttpContext.Current.Request.RequestContext);
}
private static MvcForm CreateAsyncForm(this AjaxHelper ajaxHelper, XFormParameters parameters, XForm xForm, AjaxOptions options)
{
RouteValueDictionary dictionary = new RouteValueDictionary(new
{
XFormId = xForm.Id,
action = parameters.PostAction,
controller = (parameters.PostController ?? string.Empty),
failedAction = parameters.FailedAction,
successAction = parameters.SuccessAction,
language = GetLanguage(),
contentLink = (parameters.ContentLink ?? string.Empty)
});
return ajaxHelper.BeginForm(parameters.PostAction, dictionary, options);
}
public static MvcForm BeginXForm(this AjaxHelper ajaxHelper, XForm xForm, AjaxOptions options)
{
if (xForm == null)
return (MvcForm)null;
XFormParameters parameters = XFormParameters.GetParameters((IDictionary<string, object>)ajaxHelper.ViewData);
MvcForm form = CreateAsyncForm(ajaxHelper, parameters, xForm, options);
if (ajaxHelper.ViewContext.ClientValidationEnabled)
{
ValidationRuleDescriptorCollection clientValidationRules = XFormClientValidationHelper.CreateClientValidationRules(xForm, LocalizationService.Current);
MvcFormExtensions.AddClientValidationRules(form, ajaxHelper.ViewContext.FormContext, (IEnumerable<ValidationRuleDescriptor>)clientValidationRules);
}
return form;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment