public void JavascriptAsGenericSortedListTestOptionsTest() { IDictionary <string, string> options = new SortedList <string, string> { { "key1", "option1" }, { "key2", "option2" } }; Assert.AreEqual("{key1:option1, key2:option2}", AbstractHelper.JavascriptOptions(options)); }
/// <summary> /// Implementors should return any tag/js content /// to be rendered after the form tag is closed. /// </summary> /// <param name="formId">The form id.</param> /// <returns></returns> public override string CreateBeforeFormClosed(string formId) { var stringBuilder = new StringBuilder(); if (_rules.Count > 0) { _options[JQueryOptions.Rules] = AbstractHelper.JavascriptOptions(_rules); } if (_messages.Count > 0) { _options[JQueryOptions.Messages] = AbstractHelper.JavascriptOptions(_messages); } bool isAjax; bool.TryParse(CommonUtils.ObtainEntryAndRemove( _options, JQueryOptions.IsAjax, bool.FalseString), out isAjax); if (isAjax) { var submitHandler = CommonUtils.ObtainEntryAndRemove(_options, JQueryOptions.SubmitHandler); if (submitHandler == null) { if (_ajaxOptions.Count > 0) { _options.Add( JQueryOptions.SubmitHandler, string.Concat( "function( form ) { jQuery( form ).ajaxSubmit( ", AbstractHelper.JavascriptOptions(_ajaxOptions), "); }")); } else { _options.Add( JQueryOptions.SubmitHandler, "function( form ) { jQuery( form ).ajaxSubmit(); }"); } } } MergeGroupDefinitionsWithOptions(); GenerateGroupNotEmptyValidatorCustomRule(); GenerateGroupNotEmptyValidatorCustomClass(); stringBuilder.Append("jQuery( document ).ready( function() { "); stringBuilder.AppendFormat("jQuery(\"#{0}\").validate( {1} );", formId, AbstractHelper.JavascriptOptions(_options)); if (_customRules.Count > 0) { foreach (var rule in _customRules.Values) { stringBuilder.Append(Environment.NewLine); stringBuilder.Append("jQuery.validator.addMethod('"); stringBuilder.Append(rule.Name); stringBuilder.Append("', "); stringBuilder.Append(rule.Rule); stringBuilder.Append(", '"); stringBuilder.Append(rule.ViolationMessage); stringBuilder.Append("' );"); } } if (_customClasses.Count > 0) { foreach (var pair in _customClasses) { stringBuilder.Append(Environment.NewLine); stringBuilder.Append("jQuery.validator.addClassRules({"); stringBuilder.Append(string.Format("required{0}", pair.Key)); stringBuilder.Append(": "); stringBuilder.Append(AbstractHelper.JavascriptOptions(pair.Value)); stringBuilder.Append("});"); } } stringBuilder.Append(" });"); return(AbstractHelper.ScriptBlock(stringBuilder.ToString())); }