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 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 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 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); }