/// <summary> /// Updates the given component with the current value of the property denoted in the expression and also sets the /// validation state /// </summary> /// <param name="html">Current <see cref="HtmlHelper" /></param> /// <param name="bfc">Form component to set value for</param> /// <param name="expression">Model property expression</param> /// <param name="value">Value to update component with</param> private static void UpdateComponent <TModel, TValue>(this HtmlHelper <TModel> html, IBootstrapFormComponent <TModel, TValue> bfc, Expression <Func <TModel, TValue> > expression, string value = "") { bool isValid = html.IsValidFor(expression); if (!isValid) { bfc.Wrapper.CssClasses.Add("has-error"); } if (!string.IsNullOrEmpty(value)) { bfc.SetValue(value); return; } if (html.ViewData.Model == null) { return; } object result = string.Empty; try { result = expression.Compile().DynamicInvoke(html.ViewData.Model); } catch (Exception) { // ignore } bfc.SetValue(result == null ? string.Empty : result.ToString()); }
/// <summary> /// Changes render behavior to add on /// </summary> /// <returns>The updated form control</returns> public static IFormControl <TModel, TValue> WithDefaultBehavior <TModel, TValue>( this IFormControl <TModel, TValue> control) { IBootstrapFormComponent <TModel, TValue> component = control.Component as IBootstrapFormComponent <TModel, TValue>; if (component == null) { return(control); } component.WithDefaultBehavior(); return(control); }