本文整理汇总了C#中Microsoft.AspNet.Mvc.ModelBinding.EmptyModelMetadataProvider.GetModelExplorerForType方法的典型用法代码示例。如果您正苦于以下问题:C# EmptyModelMetadataProvider.GetModelExplorerForType方法的具体用法?C# EmptyModelMetadataProvider.GetModelExplorerForType怎么用?C# EmptyModelMetadataProvider.GetModelExplorerForType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.AspNet.Mvc.ModelBinding.EmptyModelMetadataProvider
的用法示例。
在下文中一共展示了EmptyModelMetadataProvider.GetModelExplorerForType方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ModelType_UsesDeclaredType_WhenModelIsNull
public void ModelType_UsesDeclaredType_WhenModelIsNull()
{
// Arrange
var provider = new EmptyModelMetadataProvider();
var modelExplorer = provider.GetModelExplorerForType(typeof(BaseClass), model: null);
// Act
var modelType = modelExplorer.ModelType;
// Assert
Assert.Equal(typeof(BaseClass), modelType);
}
示例2: ModelType_UsesRuntimeType
public void ModelType_UsesRuntimeType()
{
// Arrange
var provider = new EmptyModelMetadataProvider();
var modelExplorer = provider.GetModelExplorerForType(typeof(BaseClass), new DerivedClass());
// Act
var modelType = modelExplorer.ModelType;
// Assert
Assert.Equal(typeof(DerivedClass), modelType);
}
示例3: GetSimpleDisplayText_WithoutSimpleDisplayProperty
public void GetSimpleDisplayText_WithoutSimpleDisplayProperty(
object model,
Type modelType,
string expectedResult)
{
// Arrange
var provider = new EmptyModelMetadataProvider();
var modelExplorer = provider.GetModelExplorerForType(modelType, model);
// Act
var result = modelExplorer.GetSimpleDisplayText();
// Assert
Assert.Equal(expectedResult, result);
}
示例4: Properties_UsesDeclaredType_WhenModelIsNull
public void Properties_UsesDeclaredType_WhenModelIsNull()
{
// Arrange
var provider = new EmptyModelMetadataProvider();
var modelExplorer = provider.GetModelExplorerForType(typeof(BaseClass), model: null);
// Act
var properties = modelExplorer.Properties.ToArray();
// Assert
Assert.Equal(1, properties.Length);
var baseProperty = Assert.Single(properties, p => p.Metadata.PropertyName == "Base1");
Assert.Equal(typeof(int), baseProperty.Metadata.ModelType);
Assert.Equal(typeof(BaseClass), baseProperty.Metadata.ContainerType);
Assert.Same(modelExplorer, baseProperty.Container);
}
示例5: Properties_UsesRuntimeType
public void Properties_UsesRuntimeType()
{
// Arrange
var provider = new EmptyModelMetadataProvider();
var modelExplorer = provider.GetModelExplorerForType(typeof(BaseClass), new DerivedClass());
// Act
var properties = modelExplorer.Properties.ToArray();
// Assert
Assert.Equal(2, properties.Length);
var baseProperty = Assert.Single(properties, p => p.Metadata.PropertyName == "Base1");
Assert.Equal(typeof(int), baseProperty.Metadata.ModelType);
Assert.Equal(typeof(DerivedClass), baseProperty.Metadata.ContainerType);
Assert.Same(modelExplorer, baseProperty.Container);
var derivedProperty = Assert.Single(properties, p => p.Metadata.PropertyName == "Derived1");
Assert.Equal(typeof(string), derivedProperty.Metadata.ModelType);
Assert.Equal(typeof(DerivedClass), derivedProperty.Metadata.ContainerType);
Assert.Same(modelExplorer, derivedProperty.Container);
}
示例6: LabelHelpers_DisplayMetadataPropertyNameForProperty
public void LabelHelpers_DisplayMetadataPropertyNameForProperty()
{
// Arrange
var propertyName = "Property1";
var provider = new EmptyModelMetadataProvider();
var modelExplorer = provider
.GetModelExplorerForType(typeof(DefaultTemplatesUtilities.ObjectTemplateModel), model: null)
.GetExplorerForProperty(propertyName);
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
helper.ViewData.ModelExplorer = modelExplorer;
// Act
var labelResult = helper.Label(expression: string.Empty);
var labelForResult = helper.LabelFor(m => m);
var labelForModelResult = helper.LabelForModel();
// Assert
Assert.Equal("<label for=\"\">HtmlEncode[[" + propertyName + "]]</label>", labelResult.ToString());
Assert.Equal("<label for=\"\">HtmlEncode[[" + propertyName + "]]</label>", labelForResult.ToString());
Assert.Equal("<label for=\"\">HtmlEncode[[" + propertyName + "]]</label>", labelForModelResult.ToString());
}
示例7: CreateModelExpression
private static ModelExpression CreateModelExpression(string name)
{
var modelMetadataProvider = new EmptyModelMetadataProvider();
return new ModelExpression(
name,
modelMetadataProvider.GetModelExplorerForType(typeof(object), model: null));
}
示例8: TagHelper_CallsGeneratorWithExpectedValues_RealModelType
public async Task TagHelper_CallsGeneratorWithExpectedValues_RealModelType(
Type modelType,
object model,
bool allowMultiple)
{
// Arrange
var contextAttributes = new Dictionary<string, object>();
var originalAttributes = new Dictionary<string, object>();
var propertyName = "Property1";
var tagName = "select";
var tagHelperContext = new TagHelperContext(
contextAttributes,
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: () =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var output = new TagHelperOutput(tagName, originalAttributes);
var metadataProvider = new EmptyModelMetadataProvider();
var modelExplorer = metadataProvider.GetModelExplorerForType(modelType, model);
var modelExpression = new ModelExpression(propertyName, modelExplorer);
var htmlGenerator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
var viewContext = TestableHtmlGenerator.GetViewContext(model, htmlGenerator.Object, metadataProvider);
var currentValues = new string[0];
htmlGenerator
.Setup(real => real.GetCurrentValues(
viewContext,
modelExplorer,
propertyName, // expression
allowMultiple))
.Returns(currentValues)
.Verifiable();
htmlGenerator
.Setup(real => real.GenerateSelect(
viewContext,
modelExplorer,
null, // optionLabel
propertyName, // expression
It.IsAny<IEnumerable<SelectListItem>>(),
currentValues,
allowMultiple,
null)) // htmlAttributes
.Returns((TagBuilder)null)
.Verifiable();
var tagHelper = new SelectTagHelper
{
For = modelExpression,
Generator = htmlGenerator.Object,
ViewContext = viewContext,
};
// Act
await tagHelper.ProcessAsync(tagHelperContext, output);
// Assert
htmlGenerator.Verify();
Assert.NotNull(viewContext.FormContext?.FormData);
var keyValuePair = Assert.Single(
viewContext.FormContext.FormData,
entry => entry.Key == SelectTagHelper.SelectedValuesFormDataKey);
Assert.Same(currentValues, keyValuePair.Value);
}
示例9: ProcessAsync_CallsGeneratorWithExpectedValues_ItemsAndAttribute
public async Task ProcessAsync_CallsGeneratorWithExpectedValues_ItemsAndAttribute(
IEnumerable<SelectListItem> inputItems,
string attributeName,
string attributeValue,
IEnumerable<SelectListItem> expectedItems)
{
// Arrange
var contextAttributes = new Dictionary<string, object>
{
// Provided for completeness. Select tag helper does not confirm AllAttributes set is consistent.
{ attributeName, attributeValue },
};
var originalAttributes = new Dictionary<string, object>
{
{ attributeName, attributeValue },
};
var propertyName = "Property1";
var expectedTagName = "select";
var tagHelperContext = new TagHelperContext(
contextAttributes,
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);
var metadataProvider = new EmptyModelMetadataProvider();
string model = null;
var modelExplorer = metadataProvider.GetModelExplorerForType(typeof(string), model);
var htmlGenerator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
var viewContext = TestableHtmlGenerator.GetViewContext(model, htmlGenerator.Object, metadataProvider);
// Simulate a (model => model) scenario. E.g. the calling helper may appear in a low-level template.
var modelExpression = new ModelExpression(string.Empty, modelExplorer);
viewContext.ViewData.TemplateInfo.HtmlFieldPrefix = propertyName;
var currentValues = new string[0];
htmlGenerator
.Setup(real => real.GetCurrentValues(
viewContext,
modelExplorer,
string.Empty, // expression
false)) // allowMultiple
.Returns(currentValues)
.Verifiable();
htmlGenerator
.Setup(real => real.GenerateSelect(
viewContext,
modelExplorer,
null, // optionLabel
string.Empty, // expression
expectedItems,
currentValues,
false, // allowMultiple
null)) // htmlAttributes
.Returns((TagBuilder)null)
.Verifiable();
var tagHelper = new SelectTagHelper
{
For = modelExpression,
Items = inputItems,
Generator = htmlGenerator.Object,
ViewContext = viewContext,
};
// Act
await tagHelper.ProcessAsync(tagHelperContext, output);
// Assert
htmlGenerator.Verify();
Assert.NotNull(viewContext.FormContext?.FormData);
var keyValuePair = Assert.Single(
viewContext.FormContext.FormData,
entry => entry.Key == SelectTagHelper.SelectedValuesFormDataKey);
Assert.Same(currentValues, keyValuePair.Value);
}
示例10: GetPropertyExplorer_DeferredModelAccess
public void GetPropertyExplorer_DeferredModelAccess()
{
// Arrange
var model = new DerivedClass()
{
Base1 = 5,
};
var provider = new EmptyModelMetadataProvider();
var modelExplorer = provider.GetModelExplorerForType(typeof(BaseClass), model);
// Change the model value after creating the explorer
var propertyExplorer = modelExplorer.GetExplorerForProperty("Base1");
model.Base1 = 17;
// Act
var propertyValue = propertyExplorer.Model;
// Assert
Assert.Equal(17, propertyValue);
}
示例11: GetPropertyExplorer_ReturnsNull_ForPropertyNotFound
public void GetPropertyExplorer_ReturnsNull_ForPropertyNotFound()
{
// Arrange
var model = new DerivedClass()
{
Base1 = 5,
};
var provider = new EmptyModelMetadataProvider();
var modelExplorer = provider.GetModelExplorerForType(typeof(BaseClass), model);
// Act
var propertyExplorer = modelExplorer.GetExplorerForProperty("BadName");
// Assert
Assert.Null(propertyExplorer);
}
示例12: GetPropertyExplorer_DeferredModelAccess_ContainerModelIsNull
public void GetPropertyExplorer_DeferredModelAccess_ContainerModelIsNull()
{
// Arrange
var provider = new EmptyModelMetadataProvider();
var modelExplorer = provider.GetModelExplorerForType(typeof(BaseClass), model: null);
var propertyExplorer = modelExplorer.GetExplorerForProperty("Base1");
// Act
var propertyValue = propertyExplorer.Model;
// Assert
Assert.Null(propertyValue);
}