public static IHtmlString LabelFor <TModel, TR>(this HtmlHelpers <TModel> html, Expression <Func <TModel, TR> > prop, object htmlAttributes) where TModel : class { var label = HtmlTagBuilder.CreateLabelFor(prop, htmlAttributes); return(label != null ? new NonEncodedHtmlString(label.ToString()) : NonEncodedHtmlString.Empty); }
public void CreateSelectForDoesNotSelectAnythingByDefault() { var model = new SelectModelTest(); var items = SelectList.CreateFrom(model.Countries, cm => cm.Id, cm => cm.Name); var tag = HtmlTagBuilder.CreateSelectFor(model, m => m.SelectedCountryId, items); Assert.IsFalse(tag.Childs.Any(child => child.Attribute("selected") != null)); }
public static IHtmlString DropDownListFor <TModel, TR>(this HtmlHelpers <TModel> html, Expression <Func <TModel, TR> > prop, SelectList items) where TModel : class { var tag = HtmlTagBuilder.CreateSelectFor(html.Model, prop, items); return(tag != null ? new NonEncodedHtmlString(tag.ToString()) : NonEncodedHtmlString.Empty); }
public void CreateSelectForWithNoModelDoesNotSelectAnything() { var model = new SelectModelTest(); var items = new SelectList(model.Countries, "Id", "Name"); var tag = HtmlTagBuilder.CreateSelectFor<SelectModelTest, int>(m => m.SelectedCountryId, items); Assert.IsFalse(tag.Childs.Any(child => child.Attribute("selected") != null)); }
public void CheckboxForBoolPropertyWithDefValueFalseAsStringIsNoChecked() { var html = new HtmlHelpers <BooleanModel>(null, null, null); var tag = HtmlTagBuilder.CreateCheckBoxFor(html, m => m.BooleanPropWithDefValueFalseAsString, null); Assert.IsNull(tag.Attribute("checked")); }
public void CreateInputElementDoesNotGenerateAttributeValueForNullModel() { var helpers = new HtmlHelpers <Person>(null, null, null); var tag = HtmlTagBuilder.CreateInputElementFor(helpers, p => p.Age, HtmlInputType.Text, null); Assert.IsNull(tag.Attribute("value")); }
public void CheckboxForBoolPropertyWithDefValueTrueIsChecked() { var html = new HtmlHelpers <BooleanModel>(null, null, null); var tag = HtmlTagBuilder.CreateCheckBoxFor(html, m => m.BooleanPropWithDefValueTrue, null); Assert.IsTrue(tag.Attribute("checked").IsWithNoValue); }
public static IHtmlString HiddenFor <TModel, TR>(this HtmlHelpers <TModel> html, Expression <Func <TModel, TR> > prop, object htmlAttributes) where TModel : class { var tag = HtmlTagBuilder.CreateInputElementFor(html, prop, HtmlInputType.Hidden, htmlAttributes); return(tag != null ? new NonEncodedHtmlString(tag.ToString()) : NonEncodedHtmlString.Empty); }
public static IHtmlString CheckBoxFor <TModel, TR>(this HtmlHelpers <TModel> html, Expression <Func <TModel, TR> > prop, object htmlAttributes) where TModel : class { var tag = HtmlTagBuilder.CreateCheckBoxFor(html, prop, htmlAttributes); html.AppendValidationResults(tag); return(tag != null ? new NonEncodedHtmlString(tag.ToString()) : NonEncodedHtmlString.Empty); }
public void CreateInputElementForNonPropertyReturnsNull() { var person = new Person(); var helpers = new HtmlHelpers <Person>(null, null, person); var tag = HtmlTagBuilder.CreateInputElementFor(helpers, p => p.Grow(), HtmlInputType.Text, null); Assert.IsNull(tag); }
public void CreateSelectForWithNoModelSelectsItemBasedOnSelectList() { var model = new SelectModelTest(); var items = new SelectList(model.Countries, "Id", "Name", 2); var tag = HtmlTagBuilder.CreateSelectFor<SelectModelTest, int>(m => m.SelectedCountryId, items); Assert.AreEqual(2.ToString(), tag.Childs.Single(child => child.Attribute("selected") != null).Attribute("value").Value); }
public void CreateSelectForSelectsItemBasedOnSelectList() { var model = new SelectModelTest(); var items = SelectList.CreateFrom(model.Countries, cm => cm.Id, cm => cm.Name); items.Last().Selected = true; var tag = HtmlTagBuilder.CreateSelectFor(model, m => m.SelectedCountryId, items); var child = tag.Childs.Single(c => c.Attribute("selected") != null); Assert.AreEqual(items.Last().Value, child.Attribute("value").Value); }
public void CheckboxForBoolPropertyIsCheckedIfPropertyIsTrue() { var model = new BooleanModel() { BooleanPropWithDefValueFalseAsString = true }; var html = new HtmlHelpers <BooleanModel>(null, null, model); var tag = HtmlTagBuilder.CreateCheckBoxFor(html, m => m.BooleanPropWithDefValueFalseAsString, null); Assert.IsTrue(tag.Attribute("checked").IsWithNoValue); }
public void CreateInputElementStoresValueOfNonStringProperty() { var person = new Person() { FirstName = "Alice", Age = 42 }; var helpers = new HtmlHelpers <Person>(null, null, person); var tag = HtmlTagBuilder.CreateInputElementFor(helpers, p => p.Age, HtmlInputType.Text, null); Assert.AreEqual(Convert.ToString(person.Age), tag.Attribute("value").Value); }
public void CreateInputElementUsesPropertyNameAsAttrName() { var person = new Person() { FirstName = "Alice" }; var helpers = new HtmlHelpers <Person>(null, null, person); var tag = HtmlTagBuilder.CreateInputElementFor(helpers, p => p.FirstName, HtmlInputType.Text, null); Assert.AreEqual("firstname", tag.Attribute("name").Value, true); }
public void CreateInputElementDoesNotGenerateAttributeValueForNullProperty() { var person = new Person() { FirstName = null }; var helpers = new HtmlHelpers <Person>(null, null, person); var tag = HtmlTagBuilder.CreateInputElementFor(helpers, p => p.FirstName, HtmlInputType.Text, null); Assert.IsNull(tag.Attribute("value")); }
public void CreateInputPasswordElementNeverStoresPropertyValue() { var person = new Person() { FirstName = "Alice" }; var helpers = new HtmlHelpers <Person>(null, null, person); var tag = HtmlTagBuilder.CreateInputElementFor(helpers, p => p.FirstName, HtmlInputType.Password, null); Assert.IsNull(tag.Attribute("value")); }
private static string GetDisplayText(IEnumerable <SelectListItem> selectList) { var stringBuilder = new StringBuilder(); foreach (SelectListItem selectListItem in selectList.Where(x => x.Selected)) { var builder = new HtmlTagBuilder("p"); builder.AddClass("form-control-static"); builder.InternalBuilder.SetInnerText(selectListItem.Text); stringBuilder.Append(builder.ToHtmlString()); } return(stringBuilder.ToString()); }
public static IHtmlString TextBoxFor <TModel, TR>(this HtmlHelpers <TModel> html, Expression <Func <TModel, TR> > prop, object htmlAttributes) where TModel : class { var tag = HtmlTagBuilder.CreateInputElementFor(html, prop, HtmlInputType.Text, htmlAttributes); AppendValidationResults(html, tag); if (html.Model != null) { AppendOldValue(html, tag); } return(tag != null ? new NonEncodedHtmlString(tag.ToString()) : NonEncodedHtmlString.Empty); }
public static IHtmlString InputElementFor <TModel, TR>(this HtmlHelpers <TModel> html, Expression <Func <TModel, TR> > prop, object htmlAttributes) where TModel : class { var property = prop.AsPropertyInfo(); if (property == null) { return(NonEncodedHtmlString.Empty); } var inputType = GetInputTypeByProperty(property); var tag = inputType == HtmlInputType.Checkbox ? HtmlTagBuilder.CreateCheckBoxFor(html, prop, htmlAttributes) : HtmlTagBuilder.CreateInputElementFor(html, prop, inputType, htmlAttributes); return(tag != null ? new NonEncodedHtmlString(tag.ToString()) : NonEncodedHtmlString.Empty); }
public static IHtmlString DropDownListFor <TModel, TS, TV, TT>(this HtmlHelpers <TModel> html, Expression <Func <TModel, IEnumerable <TS> > > source, Expression <Func <TModel, TV> > propModelValue, Expression <Func <TS, TV> > propItemValue, Expression <Func <TS, TT> > propItemText) where TModel : class { var model = html.Model; if (model == null) { return(NonEncodedHtmlString.Empty); } var sourceProperty = source.AsPropertyInfo(); var items = sourceProperty.GetValue(model) as IEnumerable <TS>; var list = SelectList.CreateFrom(items, propItemValue, propItemText); var tag = HtmlTagBuilder.CreateSelectFor(model, propModelValue, list); return(tag != null ? new NonEncodedHtmlString(tag.ToString()) : NonEncodedHtmlString.Empty); }
public static IHtmlString Render(HtmlHelper html, ITextBoxBuilder textBoxBuilder, FormGroupDisplay display, Func <object> valueFactory, Func <MvcHtmlString> hiddenField) { bool readOnlyMode = display == FormGroupDisplay.ReadOnly, labelWithHiddenFieldMode = display == FormGroupDisplay.LabelWithHiddenField; if (readOnlyMode || labelWithHiddenFieldMode) { var value = valueFactory(); var builder = new HtmlTagBuilder("p"); builder.Attr(textBoxBuilder.HtmlAttributes); builder.AddClass("form-control-static"); builder.InternalBuilder.SetInnerText(html.FormatValue(value, textBoxBuilder.Format())); if (labelWithHiddenFieldMode) { builder.InternalBuilder.InnerHtml += hiddenField(); } return(builder); } return(textBoxBuilder.AddClass("form-control")); }
public static IHtmlString Render(HtmlHelper html, ISelectListBuilder selectListBuilder, FormGroupDisplay display, string expressionText, Func <MvcHtmlString> hiddenField) { bool readOnlyMode = display == FormGroupDisplay.ReadOnly, labelWithHiddenFieldMode = display == FormGroupDisplay.LabelWithHiddenField; if (readOnlyMode || labelWithHiddenFieldMode) { var builder = new HtmlTagBuilder("div"); builder.Attr(selectListBuilder.HtmlAttributes); IEnumerable <SelectListItem> selectList = SelectListHelper.GetSelectListItems(html, selectListBuilder, expressionText); builder.InternalBuilder.InnerHtml = GetDisplayText(selectList); if (labelWithHiddenFieldMode) { builder.InternalBuilder.InnerHtml += hiddenField(); } return(MvcHtmlString.Create(builder.ToHtmlString())); } return(selectListBuilder.AddClass("form-control")); }
public void LabelForUsesDisplayNameIfFound() { var tag = HtmlTagBuilder.CreateLabelFor <LabelModel, string>(lm => lm.AnotherProperty, null); Assert.AreEqual("Another Property", tag.RawContent); }
public void LabelForUsesPropertyNameByDefault() { var tag = HtmlTagBuilder.CreateLabelFor <LabelModel, string>(lm => lm.SomeProperty, null); Assert.AreEqual("SomeProperty", tag.RawContent); }