本文整理汇总了C#中SelectList类的典型用法代码示例。如果您正苦于以下问题:C# SelectList类的具体用法?C# SelectList怎么用?C# SelectList使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SelectList类属于命名空间,在下文中一共展示了SelectList类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Constructor3SetsProperties_Value_Text
public void Constructor3SetsProperties_Value_Text()
{
// Arrange
IEnumerable items = new object[0];
// Act
SelectList selectList = new SelectList(items, "SomeValueField", "SomeTextField");
// Assert
Assert.Same(items, selectList.Items);
Assert.Equal("SomeValueField", selectList.DataValueField);
Assert.Equal("SomeTextField", selectList.DataTextField);
Assert.Null(selectList.SelectedValues);
Assert.Null(selectList.SelectedValue);
}
示例2: Constructor_SetsProperties_Items_Value_Text_SelectedValue_DisabledValues_Group
public void Constructor_SetsProperties_Items_Value_Text_SelectedValue_DisabledValues_Group()
{
// Arrange
IEnumerable items = new[]
{
new { Value = "A", Text = "Alice", Group = "AB" },
new { Value = "B", Text = "Bravo", Group = "AB" },
new { Value = "C", Text = "Charlie", Group = "C" },
};
object selectedValue = "A";
IEnumerable disabledValues = new[] { "A", "C" };
// Act
SelectList selectList = new SelectList(items,
"Value",
"Text",
"Group",
selectedValue,
disabledValues);
List<object> selectedValues = selectList.SelectedValues.Cast<object>().ToList();
// Assert
Assert.Same(items, selectList.Items);
Assert.Equal("Value", selectList.DataValueField);
Assert.Equal("Text", selectList.DataTextField);
Assert.Same(selectedValue, selectList.SelectedValue);
Assert.Single(selectedValues);
Assert.Same(selectedValue, selectedValues[0]);
Assert.Equal("Group", selectList.DataGroupField);
Assert.Same(disabledValues, selectList.DisabledValues);
}
示例3: GetViewDataWithSelectList
private static ViewDataDictionary GetViewDataWithSelectList()
{
ViewDataDictionary viewData = new ViewDataDictionary();
SelectList selectList = new SelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", "C");
viewData["foo"] = selectList;
viewData["foo.bar"] = selectList;
return viewData;
}
示例4: Constructor3SetsProperties_SelectedValue_DisabledValues
public void Constructor3SetsProperties_SelectedValue_DisabledValues()
{
// Arrange
IEnumerable items = new[] { "A", "B", "C" };
IEnumerable selectedValues = "A";
IEnumerable disabledValues = new[] { "B", "C" };
// Act
SelectList multiSelect = new SelectList(items, selectedValues, disabledValues);
// Assert
Assert.Same(items, multiSelect.Items);
Assert.Equal(new object[] { selectedValues }, multiSelect.SelectedValues);
Assert.Equal(disabledValues, multiSelect.DisabledValues);
Assert.Null(multiSelect.DataTextField);
Assert.Null(multiSelect.DataValueField);
}
示例5: DropDownListUsesExplicitValueIfNotProvidedInViewData
public void DropDownListUsesExplicitValueIfNotProvidedInViewData()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
SelectList selectList = new SelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", "C");
// Act
MvcHtmlString html = helper.DropDownList("foo", selectList, (string)null /* optionLabel */);
// Assert
Assert.Equal(
"<select id=\"foo\" name=\"foo\"><option value=\"A\">Alpha</option>" + Environment.NewLine
+ "<option value=\"B\">Bravo</option>" + Environment.NewLine
+ "<option selected=\"selected\" value=\"C\">Charlie</option>" + Environment.NewLine
+ "</select>",
html.ToHtmlString());
}
示例6: DropDownListUsesViewDataDefaultValue
public void DropDownListUsesViewDataDefaultValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(_dropDownListViewData);
SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings(), "Charlie");
// Act
MvcHtmlString html = helper.DropDownList("foo", selectList, (string)null /* optionLabel */);
// Assert
Assert.Equal(
"<select id=\"foo\" name=\"foo\"><option>Alpha</option>" + Environment.NewLine
+ "<option selected=\"selected\">Bravo</option>" + Environment.NewLine
+ "<option>Charlie</option>" + Environment.NewLine
+ "</select>",
html.ToHtmlString());
}
示例7: Constructor2SetsProperties_Items_SelectedValue
public void Constructor2SetsProperties_Items_SelectedValue()
{
// Arrange
IEnumerable items = new object[0];
object selectedValue = new object();
// Act
SelectList selectList = new SelectList(items, selectedValue);
List<object> selectedValues = selectList.SelectedValues.Cast<object>().ToList();
// Assert
Assert.Same(items, selectList.Items);
Assert.Null(selectList.DataValueField);
Assert.Null(selectList.DataTextField);
Assert.Same(selectedValue, selectList.SelectedValue);
Assert.Single(selectedValues);
Assert.Same(selectedValue, selectedValues[0]);
}
示例8: DropDownListUsesExplicitValueIfNotProvidedInViewData_Unobtrusive
public void DropDownListUsesExplicitValueIfNotProvidedInViewData_Unobtrusive()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
helper.ViewContext.ClientValidationEnabled = true;
helper.ViewContext.UnobtrusiveJavaScriptEnabled = true;
helper.ViewContext.FormContext = new FormContext();
helper.ClientValidationRuleFactory = (name, metadata) => new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } };
SelectList selectList = new SelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", "C");
// Act
MvcHtmlString html = helper.DropDownList("foo", selectList, (string)null /* optionLabel */);
// Assert
Assert.Equal(
"<select data-val=\"true\" data-val-type=\"error\" id=\"foo\" name=\"foo\"><option value=\"A\">Alpha</option>" + Environment.NewLine
+ "<option value=\"B\">Bravo</option>" + Environment.NewLine
+ "<option selected=\"selected\" value=\"C\">Charlie</option>" + Environment.NewLine
+ "</select>",
html.ToHtmlString());
}
示例9: DropDownListWithObjectDictionaryWithUnderscoresAndSelectListNoOptionLabel
public void DropDownListWithObjectDictionaryWithUnderscoresAndSelectListNoOptionLabel()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
// Act
MvcHtmlString html = helper.DropDownList("foo", selectList, HtmlHelperTest.AttributesObjectUnderscoresDictionary);
// Assert
Assert.Equal(
"<select foo-baz=\"BazObjValue\" id=\"foo\" name=\"foo\"><option>Alpha</option>" + Environment.NewLine
+ "<option>Bravo</option>" + Environment.NewLine
+ "<option>Charlie</option>" + Environment.NewLine
+ "</select>",
html.ToHtmlString());
}
示例10: SelectHelpersUseCurrentCultureToConvertValues
public void SelectHelpersUseCurrentCultureToConvertValues()
{
// Arrange
HtmlHelper defaultValueHelper = MvcHelper.GetHtmlHelper(new ViewDataDictionary
{
{ "foo", new[] { new DateTime(1900, 1, 1, 0, 0, 1) } },
{ "bar", new DateTime(1900, 1, 1, 0, 0, 1) }
});
HtmlHelper helper = MvcHelper.GetHtmlHelper();
SelectList selectList = new SelectList(GetSampleCultureAnonymousObjects(), "Date", "FullWord", new DateTime(1900, 1, 1, 0, 0, 0));
var tests = new[]
{
// DropDownList(name, selectList, optionLabel)
new
{
Html = "<select id=\"foo\" name=\"foo\"><option selected=\"selected\" value=\"01/01/1900 00:00:00\">Alpha</option>" + Environment.NewLine
+ "<option value=\"01/01/1900 00:00:01\">Bravo</option>" + Environment.NewLine
+ "<option value=\"01/01/1900 00:00:02\">Charlie</option>" + Environment.NewLine
+ "</select>",
Action = new Func<MvcHtmlString>(() => helper.DropDownList("foo", selectList, (string)null))
},
// DropDownList(name, selectList, optionLabel) (With default value selected from ViewData)
new
{
Html = "<select id=\"bar\" name=\"bar\"><option value=\"01/01/1900 00:00:00\">Alpha</option>" + Environment.NewLine
+ "<option selected=\"selected\" value=\"01/01/1900 00:00:01\">Bravo</option>" + Environment.NewLine
+ "<option value=\"01/01/1900 00:00:02\">Charlie</option>" + Environment.NewLine
+ "</select>",
Action = new Func<MvcHtmlString>(() => defaultValueHelper.DropDownList("bar", selectList, (string)null))
},
// ListBox(name, selectList)
new
{
Html = "<select id=\"foo\" multiple=\"multiple\" name=\"foo\"><option selected=\"selected\" value=\"01/01/1900 00:00:00\">Alpha</option>" + Environment.NewLine
+ "<option value=\"01/01/1900 00:00:01\">Bravo</option>" + Environment.NewLine
+ "<option value=\"01/01/1900 00:00:02\">Charlie</option>" + Environment.NewLine
+ "</select>",
Action = new Func<MvcHtmlString>(() => helper.ListBox("foo", selectList))
},
// ListBox(name, selectList) (With default value selected from ViewData)
new
{
Html = "<select id=\"foo\" multiple=\"multiple\" name=\"foo\"><option value=\"01/01/1900 00:00:00\">Alpha</option>" + Environment.NewLine
+ "<option selected=\"selected\" value=\"01/01/1900 00:00:01\">Bravo</option>" + Environment.NewLine
+ "<option value=\"01/01/1900 00:00:02\">Charlie</option>" + Environment.NewLine
+ "</select>",
Action = new Func<MvcHtmlString>(() => defaultValueHelper.ListBox("foo", selectList))
}
};
// Act && Assert
foreach (var test in tests)
{
Assert.Equal(test.Html, test.Action().ToHtmlString());
}
}
示例11: DropDownListWithErrorsAndCustomClass
public void DropDownListWithErrorsAndCustomClass()
{
// Arrange
SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
ViewDataDictionary viewData = GetViewDataWithErrors();
HtmlHelper helper = MvcHelper.GetHtmlHelper(viewData);
// Act
MvcHtmlString html = helper.DropDownList("foo", selectList, null /* optionLabel */, new { @class = "foo-class" });
// Assert
Assert.Equal(
"<select class=\"input-validation-error foo-class\" id=\"foo\" name=\"foo\"><option>Alpha</option>" + Environment.NewLine
+ "<option selected=\"selected\">Bravo</option>" + Environment.NewLine
+ "<option>Charlie</option>" + Environment.NewLine
+ "</select>",
html.ToHtmlString());
}
示例12: ListBoxForUsesLambdaDefaultValue
public void ListBoxForUsesLambdaDefaultValue()
{
// Arrange
FooArrayModel model = new FooArrayModel { foo = new[] { "Bravo" } };
HtmlHelper<FooArrayModel> helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary<FooArrayModel>(model));
SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
// Act
MvcHtmlString html = helper.ListBoxFor(m => m.foo, selectList);
// Assert
Assert.Equal(
"<select id=\"foo\" multiple=\"multiple\" name=\"foo\"><option>Alpha</option>" + Environment.NewLine
+ "<option selected=\"selected\">Bravo</option>" + Environment.NewLine
+ "<option>Charlie</option>" + Environment.NewLine
+ "</select>",
html.ToHtmlString());
}
示例13: DropDownListUsesViewDataDefaultValueNoOptionLabel
public void DropDownListUsesViewDataDefaultValueNoOptionLabel()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(_dropDownListViewData);
SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings(), "Charlie");
// Act
MvcHtmlString html = helper.DropDownList("foo", selectList);
// Assert
Assert.Equal(
@"<select id=""foo"" name=""foo""><option>Alpha</option>
<option selected=""selected"">Bravo</option>
<option>Charlie</option>
</select>",
html.ToHtmlString());
}
示例14: DropDownListForWithPrefixAndEmptyName
public void DropDownListForWithPrefixAndEmptyName()
{
// Arrange
HtmlHelper<FooModel> helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary<FooModel>());
SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
// Act
MvcHtmlString html = helper.DropDownListFor(m => m, selectList, HtmlHelperTest.AttributesObjectDictionary);
// Assert
Assert.Equal(
@"<select baz=""BazObjValue"" id=""MyPrefix"" name=""MyPrefix""><option>Alpha</option>
<option>Bravo</option>
<option>Charlie</option>
</select>",
html.ToHtmlString());
}
示例15: CreateUser
private static void CreateUser(ActiveEventArgs e)
{
SelectList list = new SelectList {CssClass = "smallSelectList"};
// Adding first static item...
ListItem top = new ListItem(Language.Instance["ChooseUser", null, "Choose User..."], "0");
list.Items.Add(top);
string curValue = e.Params["Value"].Get<string>();
foreach (User idxUser in ActiveType<User>.Select())
{
ListItem i = new ListItem(idxUser.Username, idxUser.Username);
list.Items.Add(i);
if (curValue == idxUser.Username)
{
i.Selected = true;
}
}
list.SelectedIndexChanged +=
delegate(object sender, EventArgs e2)
{
SelectList lst = sender as SelectList;
if (lst == null)
return;
string[] xtra = lst.Xtra.Split('|');
int rowId = int.Parse(xtra[0]);
Whiteboard.Row row = ActiveType<Whiteboard.Row>.SelectByID(rowId);
Whiteboard.Cell cell = row.Cells.Find(
delegate(Whiteboard.Cell idx)
{
return idx.Column.Caption == xtra[1];
});
cell.Value = lst.SelectedItem.Value;
cell.Save();
UpdateGridValue(e.Params["DataSource"].Value as Node, xtra[0], xtra[1], cell.Value);
};
// Sending back our Control
e.Params["Control"].Value = list;
}