public void AddOptions_Enum_SelectedValues() { var tag = new SelectTag() .AddOptions( OptionsList.CreateForEnum <Enum1>() .SelectedValues(new string[] { Enum1.Option2.ToString() }) .Configure((x, op) => op.Data("customdata", x.Value))); Assert.Equal(2, tag.Children.Count()); var firstChild = tag.Children.First(); var secondChild = tag.Children.Skip(1).First(); Assert.Equal("Option1", firstChild.Text()); Assert.True(firstChild.ValueIsEqual(Enum1.Option1.ToString())); Assert.False(firstChild.HasAttr("selected")); Assert.Equal(1, firstChild.Data("customdata")); Assert.Equal("Option2", secondChild.Text()); Assert.True(secondChild.ValueIsEqual(Enum1.Option2.ToString())); Assert.True(secondChild.HasAttr("selected")); Assert.Equal(2, secondChild.Data("customdata")); }
public void Select_Null_DefaultValue() { var model = new Model1() { PropInt = null }; var helper = new HtmlTagHelper(model); var name = "PropInt"; var tag = helper.Select( name, OptionsList.CreateForEnum <Enum1>(), defaultValue: 2); AssertValid(tag, "select", name); var firstChild = tag.Children.First(); var secondChild = tag.Children.Skip(1).First(); var thirdChild = tag.Children.Skip(2).First(); Assert.Equal("Option1", firstChild.Text()); Assert.True(firstChild.ValueIsEqual(1)); Assert.False(firstChild.HasAttr("selected")); Assert.Equal("Option2", secondChild.Text()); Assert.True(secondChild.ValueIsEqual(2)); Assert.True(secondChild.HasAttr("selected")); Assert.Equal("Option3", thirdChild.Text()); Assert.True(thirdChild.ValueIsEqual(3)); Assert.False(thirdChild.HasAttr("selected")); }
public void AddOptions_Enum_Localized() { var prevResource = Resources.Current; try { Resources.Register(new TypedClassResourceProvider(typeof(Enum1Resources))); var tag = new SelectTag() .AddOptions( OptionsList.CreateForEnum <Enum1>() .SelectedValue(Enum1.Option2.ToString()) .Configure((x, op) => op.Data("customdata", x.Value))); Assert.Equal(2, tag.Children.Count()); var firstChild = tag.Children.First(); var secondChild = tag.Children.Skip(1).First(); Assert.Equal("Option 1", firstChild.Text()); Assert.True(firstChild.ValueIsEqual(Enum1.Option1.ToString())); Assert.False(firstChild.HasAttr("selected")); Assert.Equal(1, firstChild.Data("customdata")); Assert.Equal("Option2", secondChild.Text()); Assert.True(secondChild.ValueIsEqual(Enum1.Option2.ToString())); Assert.True(secondChild.HasAttr("selected")); Assert.Equal(2, secondChild.Data("customdata")); } finally { Resources.Register(prevResource); } }
public void SelectFor_NotNull_DefaultValue() { var model = new Model1() { PropInt = 1 }; var helper = new HtmlTagHelper <Model1>(model); var propExpression = Expr((Model1 m) => m.PropInt); var tag = helper.SelectFor( propExpression, OptionsList.CreateForEnum <Enum1>(), defaultValue: 2); AssertValid(tag, "select", "PropInt"); var firstChild = tag.Children.First(); var secondChild = tag.Children.Skip(1).First(); var thirdChild = tag.Children.Skip(2).First(); Assert.Equal("Option1", firstChild.Text()); Assert.True(firstChild.ValueIsEqual(1)); Assert.True(firstChild.HasAttr("selected")); Assert.Equal("Option2", secondChild.Text()); Assert.True(secondChild.ValueIsEqual(2)); Assert.False(secondChild.HasAttr("selected")); Assert.Equal("Option3", thirdChild.Text()); Assert.True(thirdChild.ValueIsEqual(3)); Assert.False(thirdChild.HasAttr("selected")); }
public void AddOptions_Enum_Localized() { var prevResource = Resources.Current; try { Resources.Register(new TypedClassResourceProvider(typeof(Enum1Resources))); var tag = new CheckBoxListTag(DEFAULT_NAME) .AddOptions( OptionsList.CreateForEnum <Enum1>() .SelectedValue(2) .Configure((x, op) => op.Data("customdata", x.Value))); Assert.Equal(2, tag.Options.Count()); var firstChild = tag.Options.First(); var secondChild = tag.Options.Skip(1).First(); AssertValidOption(firstChild, name: DEFAULT_NAME, display: "Option 1", value: 1, isChecked: false); AssertValidOption(secondChild, name: DEFAULT_NAME, display: "Option2", value: 2, isChecked: true); Assert.Equal(1, firstChild.Data("customdata")); Assert.Equal(2, secondChild.Data("customdata")); } finally { Resources.Register(prevResource); } }
public static OptionsList <EnumOption <TEnum, int> > GetOptionsListFor <TModel, TEnum>( this HtmlHelper <TModel> helper, Expression <System.Func <TModel, TEnum[]> > nameExpression, Func <EnumOption <TEnum, int>, string> display = null) where TEnum : struct, IConvertible { var selectedValues = nameExpression.Compile().Invoke(helper.ViewData.Model); var selectedValuesInt = selectedValues == null ? null : selectedValues.Cast <int>(); return(OptionsList.CreateForEnum <TEnum>(display: display) .SelectedValues(selectedValuesInt)); }
public void CheckBoxListFor_Null() { var model = new Model1() { PropIntList = null }; var helper = new HtmlTagHelper <Model1>(model); var propExpression = Expr((Model1 m) => m.PropIntList); var tag = helper.CheckBoxListFor( propExpression, OptionsList.CreateForEnum <Enum1>()); Assert.Equal(3, tag.Children.Count()); CheckBoxListTagTest.AssertValidOption(tag.Options.Skip(0).First(), name: "PropIntList", display: "Option1", value: 1, isChecked: false); CheckBoxListTagTest.AssertValidOption(tag.Options.Skip(1).First(), name: "PropIntList", display: "Option2", value: 2, isChecked: false); CheckBoxListTagTest.AssertValidOption(tag.Options.Skip(2).First(), name: "PropIntList", display: "Option3", value: 3, isChecked: false); }
public void CheckBoxList_Null() { var model = new Model1() { PropIntList = null }; var helper = new HtmlTagHelper(model); var name = "PropIntList"; var tag = helper.CheckBoxList( name, OptionsList.CreateForEnum <Enum1>()); Assert.Equal(3, tag.Children.Count()); CheckBoxListTagTest.AssertValidOption(tag.Options.Skip(0).First(), name: name, display: "Option1", value: 1, isChecked: false); CheckBoxListTagTest.AssertValidOption(tag.Options.Skip(1).First(), name: name, display: "Option2", value: 2, isChecked: false); CheckBoxListTagTest.AssertValidOption(tag.Options.Skip(2).First(), name: name, display: "Option3", value: 3, isChecked: false); }
public void AddOptions_Enum_SelectedValues() { var tag = new CheckBoxListTag(DEFAULT_NAME) .AddOptions( OptionsList.CreateForEnum <Enum1>() .SelectedValues(Coll.Array(2)) .Configure((x, op) => op.Data("customdata", x.Value))); Assert.Equal(2, tag.Options.Count()); var firstChild = tag.Options.First(); var secondChild = tag.Options.Skip(1).First(); AssertValidOption(firstChild, name: DEFAULT_NAME, display: "Option1", value: 1, isChecked: false); AssertValidOption(secondChild, name: DEFAULT_NAME, display: "Option2", value: 2, isChecked: true); Assert.Equal(1, firstChild.Data("customdata")); Assert.Equal(2, secondChild.Data("customdata")); }
public void AddOptions_Enum_Display() { var tag = new SelectTag() .AddOptions( OptionsList.CreateForEnum<Enum1>(display: x => x.Name + "-" + x.Value) .SelectedValue(2) .Configure((x, op) => op.Data("customdata", x.Value))); Assert.Equal(2, tag.Children.Count()); var firstChild = tag.Children.First(); var secondChild = tag.Children.Skip(1).First(); Assert.Equal("Option1-1", firstChild.Text()); Assert.True(firstChild.ValueIsEqual(1)); Assert.False(firstChild.HasAttr("selected")); Assert.Equal(1, firstChild.Data("customdata")); Assert.Equal("Option2-2", secondChild.Text()); Assert.True(secondChild.ValueIsEqual(2)); Assert.True(secondChild.HasAttr("selected")); Assert.Equal(2, secondChild.Data("customdata")); }