本文整理汇总了C#中Microsoft.AspNet.Mvc.Rendering.SelectList.Select方法的典型用法代码示例。如果您正苦于以下问题:C# SelectList.Select方法的具体用法?C# SelectList.Select怎么用?C# SelectList.Select使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.AspNet.Mvc.Rendering.SelectList
的用法示例。
在下文中一共展示了SelectList.Select方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessAsyncInTemplate_WithItems_GeneratesExpectedOutput_DoesNotChangeSelectList
public async Task ProcessAsyncInTemplate_WithItems_GeneratesExpectedOutput_DoesNotChangeSelectList(
object model,
Type containerType,
Func<object> modelAccessor,
NameAndId nameAndId,
string expectedOptions)
{
// Arrange
var originalAttributes = new Dictionary<string, object>
{
{ "class", "form-control" },
};
var originalPostContent = "original content";
var expectedAttributes = new Dictionary<string, object>(originalAttributes)
{
{ "id", nameAndId.Id },
{ "name", nameAndId.Name },
{ "valid", "from validation attributes" },
};
var expectedPreContent = "original pre-content";
var expectedContent = "original content";
var expectedPostContent = originalPostContent + expectedOptions;
var expectedTagName = "select";
var metadataProvider = new TestModelMetadataProvider();
var containerMetadata = metadataProvider.GetMetadataForType(containerType);
var containerExplorer = metadataProvider.GetModelExplorerForType(containerType, model);
var propertyMetadata = metadataProvider.GetMetadataForProperty(containerType, "Text");
var modelExplorer = containerExplorer.GetExplorerForExpression(propertyMetadata, modelAccessor());
var modelExpression = new ModelExpression(name: string.Empty, modelExplorer: modelExplorer);
var tagHelperContext = new TagHelperContext(
allAttributes: new Dictionary<string, object>(),
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: () =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var output = new TagHelperOutput(expectedTagName, originalAttributes)
{
SelfClosing = true
};
output.PreContent.SetContent(expectedPreContent);
output.Content.SetContent(expectedContent);
output.PostContent.SetContent(originalPostContent);
var htmlGenerator = new TestableHtmlGenerator(metadataProvider)
{
ValidationAttributes =
{
{ "valid", "from validation attributes" },
}
};
var viewContext = TestableHtmlGenerator.GetViewContext(model, htmlGenerator, metadataProvider);
viewContext.ViewData.TemplateInfo.HtmlFieldPrefix = nameAndId.Name;
var items = new SelectList(new[] { "", "outer text", "inner text", "other text" });
var savedDisabled = items.Select(item => item.Disabled).ToList();
var savedGroup = items.Select(item => item.Group).ToList();
var savedSelected = items.Select(item => item.Selected).ToList();
var savedText = items.Select(item => item.Text).ToList();
var savedValue = items.Select(item => item.Value).ToList();
var tagHelper = new SelectTagHelper
{
For = modelExpression,
Generator = htmlGenerator,
Items = items,
ViewContext = viewContext,
};
// Act
await tagHelper.ProcessAsync(tagHelperContext, output);
// Assert
Assert.True(output.SelfClosing);
Assert.Equal(expectedAttributes, output.Attributes);
Assert.Equal(expectedPreContent, output.PreContent.GetContent());
Assert.Equal(expectedContent, output.Content.GetContent());
Assert.Equal(expectedPostContent, output.PostContent.GetContent());
Assert.Equal(expectedTagName, output.TagName);
Assert.NotNull(viewContext.FormContext?.FormData);
Assert.Single(
viewContext.FormContext.FormData,
entry => entry.Key == SelectTagHelper.SelectedValuesFormDataKey);
Assert.Equal(savedDisabled, items.Select(item => item.Disabled));
Assert.Equal(savedGroup, items.Select(item => item.Group));
Assert.Equal(savedSelected, items.Select(item => item.Selected));
Assert.Equal(savedText, items.Select(item => item.Text));
Assert.Equal(savedValue, items.Select(item => item.Value));
}
示例2: ProcessAsync_WithItems_GeneratesExpectedOutput_DoesNotChangeSelectList
public async Task ProcessAsync_WithItems_GeneratesExpectedOutput_DoesNotChangeSelectList(
object model,
Type containerType,
Func<object> modelAccessor,
NameAndId nameAndId,
string expectedOptions)
{
// Arrange
var originalAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var originalPostContent = "original content";
var expectedAttributes = new TagHelperAttributeList(originalAttributes)
{
{ "id", nameAndId.Id },
{ "name", nameAndId.Name },
{ "valid", "from validation attributes" },
};
var expectedPreContent = "original pre-content";
var expectedContent = "original content";
var expectedPostContent = originalPostContent + expectedOptions;
var expectedTagName = "select";
var metadataProvider = new TestModelMetadataProvider();
var containerMetadata = metadataProvider.GetMetadataForType(containerType);
var containerExplorer = metadataProvider.GetModelExplorerForType(containerType, model);
var propertyMetadata = metadataProvider.GetMetadataForProperty(containerType, "Text");
var modelExplorer = containerExplorer.GetExplorerForExpression(propertyMetadata, modelAccessor());
var modelExpression = new ModelExpression(nameAndId.Name, modelExplorer);
var tagHelperContext = new TagHelperContext(
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test");
var output = new TagHelperOutput(
expectedTagName,
originalAttributes,
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.AppendHtml("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
})
{
TagMode = TagMode.SelfClosing,
};
output.PreContent.AppendHtml(expectedPreContent);
output.Content.AppendHtml(expectedContent);
output.PostContent.AppendHtml(originalPostContent);
var htmlGenerator = new TestableHtmlGenerator(metadataProvider)
{
ValidationAttributes =
{
{ "valid", "from validation attributes" },
}
};
var viewContext = TestableHtmlGenerator.GetViewContext(model, htmlGenerator, metadataProvider);
var items = new SelectList(new[] { "", "outer text", "inner text", "other text" });
var savedDisabled = items.Select(item => item.Disabled).ToList();
var savedGroup = items.Select(item => item.Group).ToList();
var savedSelected = items.Select(item => item.Selected).ToList();
var savedText = items.Select(item => item.Text).ToList();
var savedValue = items.Select(item => item.Value).ToList();
var tagHelper = new SelectTagHelper(htmlGenerator)
{
For = modelExpression,
Items = items,
ViewContext = viewContext,
};
// Act
tagHelper.Init(tagHelperContext);
await tagHelper.ProcessAsync(tagHelperContext, output);
// Assert
Assert.Equal(TagMode.SelfClosing, output.TagMode);
Assert.Equal(expectedAttributes, output.Attributes);
Assert.Equal(expectedPreContent, output.PreContent.GetContent());
Assert.Equal(expectedContent, output.Content.GetContent());
Assert.Equal(expectedPostContent, HtmlContentUtilities.HtmlContentToString(output.PostContent));
Assert.Equal(expectedTagName, output.TagName);
Assert.Single(
tagHelperContext.Items,
entry => (Type)entry.Key == typeof(SelectTagHelper));
Assert.Equal(savedDisabled, items.Select(item => item.Disabled));
Assert.Equal(savedGroup, items.Select(item => item.Group));
Assert.Equal(savedSelected, items.Select(item => item.Selected));
Assert.Equal(savedText, items.Select(item => item.Text));
Assert.Equal(savedValue, items.Select(item => item.Value));
//.........这里部分代码省略.........