/// <summary> /// Renders a form suitable for BForms fields /// </summary> public static BsMvcForm BsBeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, FormMethod method, IDictionary <string, object> htmlAttributes, BsTheme theme) { var form = htmlHelper.BeginForm(actionName, controllerName, routeValues, method, htmlAttributes); var tag = new TagBuilder("div"); tag.AddCssClass("row"); tag.AddCssClass("form_container"); tag.AddCssClass("loading"); tag.AddCssClass(theme.GetDescription()); var myForm = new BsMvcForm(htmlHelper.ViewContext, form, tag.ToString(TagRenderMode.StartTag), tag.ToString(TagRenderMode.EndTag)); return(myForm); }
/// <summary> /// Renders a form suitable for BForms fields /// </summary> public static BsMvcForm BsBeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, FormMethod method, IDictionary<string, object> htmlAttributes, BsTheme theme) { var form = htmlHelper.BeginForm(actionName, controllerName, routeValues, method, htmlAttributes); var tag = new TagBuilder("div"); tag.AddCssClass("row"); tag.AddCssClass("form_container"); tag.AddCssClass("loading"); tag.AddCssClass(theme.GetDescription()); var myForm = new BsMvcForm(htmlHelper.ViewContext, form, tag.ToString(TagRenderMode.StartTag), tag.ToString(TagRenderMode.EndTag)); return myForm; }
public string RenderForm <TFormModel>(TFormModel formModel, BsTheme theme = BsTheme.Default, bool wrapInForm = true, string propertyName = null) where TFormModel : class { if (formModel == null) { throw new Exception("The form model passed as an argument cannot be null"); } var formString = String.Empty; var formButtonsString = String.Empty; var formBuilder = new TagBuilder("form"); var formContainerBuilder = new TagBuilder("div"); formBuilder.MergeAttribute("novalidate", "novalidate"); formContainerBuilder.AddCssClass("form_container " + theme.GetDescription()); var properties = formModel.GetType().GetProperties(); var bsControlProperties = from prop in formModel.GetType().GetProperties() let bsControl = prop.GetCustomAttributes(typeof(BsControlAttribute), true) let required = prop.GetCustomAttributes(typeof(RequiredAttribute), true) let display = prop.GetCustomAttributes(typeof(DisplayAttribute), true) let formGroup = prop.GetCustomAttributes(typeof(FormGroup), true) where bsControl.Length == 1 select new FormControlPropertyMetadata { PropertyInfo = prop, FormGroupAttribute = formGroup.FirstOrDefault() as FormGroup, RequiredAttribute = required.FirstOrDefault() as RequiredAttribute, DisplayAttribute = display.FirstOrDefault() as DisplayAttribute, BsControlAttribute = bsControl.FirstOrDefault() as BsControlAttribute }; foreach (var property in bsControlProperties) { formString += RenderFormGroup(property, formModel); } var buttonsProperties = from prop in formModel.GetType().GetProperties() where prop.GetCustomAttributes(typeof(FormButtons), true).Any() select prop; var buttons = buttonsProperties.Select(prop => prop.GetValue(formModel)).FirstOrDefault(); if (buttons != null) { var buttonsList = buttons as IEnumerable <BsButtonModel>; if (buttonsList == null) { throw new Exception("Properties decorated with the BsButtonModel attribute must be of type IEnumerable<BsButtonModel>"); } formButtonsString = RenderFormButtons(buttonsList); } formContainerBuilder.InnerHtml = formString + formButtonsString; if (wrapInForm) { formBuilder.InnerHtml = formContainerBuilder.ToString(); formBuilder.MergeAttribute("data-property-name", propertyName); return(formBuilder.ToString()); } return(formContainerBuilder.ToString()); }