kreta/KretaWeb/Helpers/FormExtensions.cs
2024-03-13 00:33:46 +01:00

51 lines
1.8 KiB
C#

using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
namespace Kreta.Web.Helpers
{
public static class FormExtensions
{
public static MvcForm KretaForm(this HtmlHelper htmlHelper, string name, Dictionary<string, object> htmlAttributes = null)
{
if (htmlAttributes == null)
htmlAttributes = new Dictionary<string, object>();
htmlAttributes.Add("id", name);
htmlAttributes.Add("onsubmit", "return false");
var mvcForm = htmlHelper.BeginForm(null, null, FormMethod.Get, htmlAttributes);
var formName = "#" + name;
StringBuilder sb = new StringBuilder();
sb.Append("<script>$.validator.setDefaults({ ignore: [] });</script>");
sb.AppendFormat("<script>KretaForm.validate($('{0}'));</script>", formName);
htmlHelper.ViewContext.Writer.Write(sb.ToString());
sb.Append("</form>");
return mvcForm;
}
public static MvcForm KretaForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, string name, Dictionary<string, object> htmlAttributes = null)
{
if (htmlAttributes == null)
htmlAttributes = new Dictionary<string, object>();
htmlAttributes.Add("id", name);
var mvcForm = htmlHelper.BeginForm(actionName, controllerName, method, htmlAttributes);
var formName = "#" + name;
StringBuilder sb = new StringBuilder();
sb.Append("<script>$.validator.setDefaults({ ignore: [] });</script>");
sb.AppendFormat("<script>KretaForm.validate($('{0}'));</script>", formName);
htmlHelper.ViewContext.Writer.Write(sb.ToString());
return mvcForm;
}
}
}