本文整理汇总了C#中TagHelperAttributeList.SetAttribute方法的典型用法代码示例。如果您正苦于以下问题:C# TagHelperAttributeList.SetAttribute方法的具体用法?C# TagHelperAttributeList.SetAttribute怎么用?C# TagHelperAttributeList.SetAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TagHelperAttributeList
的用法示例。
在下文中一共展示了TagHelperAttributeList.SetAttribute方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessAsync_CallsGenerateTextBox_WithExpectedParameters
public async Task ProcessAsync_CallsGenerateTextBox_WithExpectedParameters(
string dataTypeName,
string inputTypeName,
string model)
{
// Arrange
var contextAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
if (!string.IsNullOrEmpty(inputTypeName))
{
contextAttributes.SetAttribute("type", inputTypeName); // Support restoration of type attribute, if any.
}
var expectedAttributes = new TagHelperAttributeList
{
{ "class", "form-control text-control" },
{ "type", inputTypeName ?? "text" }, // Generator restores type attribute; adds "text" if none.
};
var expectedPreContent = "original pre-content";
var expectedContent = "original content";
var expectedPostContent = "original post-content";
var expectedTagName = "not-input";
var context = new TagHelperContext(
allAttributes: contextAttributes,
items: new Dictionary<object, object>(),
uniqueId: "test");
var originalAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(
expectedTagName,
originalAttributes,
getChildContentAsync: (useCachedResult, encoder) =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
})
{
TagMode = TagMode.StartTagOnly,
};
output.PreContent.SetContent(expectedPreContent);
output.Content.SetContent(expectedContent);
output.PostContent.SetContent(expectedPostContent);
var metadataProvider = new TestModelMetadataProvider();
metadataProvider.ForProperty<Model>("Text").DisplayDetails(dd => dd.DataTypeName = dataTypeName);
var htmlGenerator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
var tagHelper = GetTagHelper(
htmlGenerator.Object,
model,
nameof(Model.Text),
metadataProvider: metadataProvider);
tagHelper.InputTypeName = inputTypeName;
var tagBuilder = new TagBuilder("input")
{
Attributes =
{
{ "class", "text-control" },
},
};
htmlGenerator
.Setup(mock => mock.GenerateTextBox(
tagHelper.ViewContext,
tagHelper.For.ModelExplorer,
tagHelper.For.Name,
model, // value
null, // format
It.Is<Dictionary<string, object>>(m => m.ContainsKey("type")))) // htmlAttributes
.Returns(tagBuilder)
.Verifiable();
// Act
await tagHelper.ProcessAsync(context, output);
// Assert
htmlGenerator.Verify();
Assert.Equal(TagMode.StartTagOnly, output.TagMode);
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);
}
示例2: ProcessAsync_CallsGenerateRadioButton_WithExpectedParameters
public async Task ProcessAsync_CallsGenerateRadioButton_WithExpectedParameters(
string inputTypeName,
string model)
{
// Arrange
var value = "match"; // Real generator would use this for comparison with For.Metadata.Model.
var contextAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
{ "value", value },
};
if (!string.IsNullOrEmpty(inputTypeName))
{
contextAttributes.SetAttribute("type", inputTypeName); // Support restoration of type attribute, if any.
}
var expectedAttributes = new TagHelperAttributeList
{
{ "class", "form-control radio-control" },
{ "value", value },
{ "type", inputTypeName ?? "radio" }, // Generator restores type attribute; adds "radio" if none.
};
var expectedPreContent = "original pre-content";
var expectedContent = "original content";
var expectedPostContent = "original post-content";
var expectedTagName = "not-input";
var context = new TagHelperContext(
allAttributes: contextAttributes,
items: new Dictionary<object, object>(),
uniqueId: "test");
var originalAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(
expectedTagName,
originalAttributes,
getChildContentAsync: (useCachedResult, encoder) =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
})
{
TagMode = TagMode.StartTagOnly,
};
output.PreContent.SetContent(expectedPreContent);
output.Content.SetContent(expectedContent);
output.PostContent.SetContent(expectedPostContent);
var htmlGenerator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
var tagHelper = GetTagHelper(htmlGenerator.Object, model, nameof(Model.Text));
tagHelper.InputTypeName = inputTypeName;
tagHelper.Value = value;
var tagBuilder = new TagBuilder("input")
{
Attributes =
{
{ "class", "radio-control" },
},
};
htmlGenerator
.Setup(mock => mock.GenerateRadioButton(
tagHelper.ViewContext,
tagHelper.For.ModelExplorer,
tagHelper.For.Name,
value,
null, // isChecked
null)) // htmlAttributes
.Returns(tagBuilder)
.Verifiable();
// Act
await tagHelper.ProcessAsync(context, output);
// Assert
htmlGenerator.Verify();
Assert.Equal(TagMode.StartTagOnly, output.TagMode);
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);
}