本文整理汇总了C#中Microsoft.AspNet.Mvc.ModelBinding.EmptyModelMetadataProvider类的典型用法代码示例。如果您正苦于以下问题:C# EmptyModelMetadataProvider类的具体用法?C# EmptyModelMetadataProvider怎么用?C# EmptyModelMetadataProvider使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
EmptyModelMetadataProvider类属于Microsoft.AspNet.Mvc.ModelBinding命名空间,在下文中一共展示了EmptyModelMetadataProvider类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryUpdateModel_ReturnsFalse_IfBinderReturnsNull
public async Task TryUpdateModel_ReturnsFalse_IfBinderReturnsNull()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var binder = new Mock<IModelBinder>();
binder.Setup(b => b.BindModelAsync(It.IsAny<ModelBindingContext>()))
.Returns(Task.FromResult<ModelBindingResult>(null));
var model = new MyModel();
// Act
var result = await ModelBindingHelper.TryUpdateModelAsync(
model,
null,
Mock.Of<HttpContext>(),
new ModelStateDictionary(),
metadataProvider,
GetCompositeBinder(binder.Object),
Mock.Of<IValueProvider>(),
new List<IInputFormatter>(),
new DefaultObjectValidator(new IExcludeTypeValidationFilter[0], metadataProvider),
Mock.Of<IModelValidatorProvider>());
// Assert
Assert.False(result);
Assert.Null(model.MyProperty);
}
示例2: JsonPatchInputFormatter_ReadsMultipleOperations_Successfully
public async Task JsonPatchInputFormatter_ReadsMultipleOperations_Successfully()
{
// Arrange
var formatter = new JsonPatchInputFormatter();
var content = "[{\"op\": \"add\", \"path\" : \"Customer/Name\", \"value\":\"John\"}," +
"{\"op\": \"remove\", \"path\" : \"Customer/Name\"}]";
var contentBytes = Encoding.UTF8.GetBytes(content);
var modelState = new ModelStateDictionary();
var httpContext = GetHttpContext(contentBytes);
var provider = new EmptyModelMetadataProvider();
var metadata = provider.GetMetadataForType(typeof(JsonPatchDocument<Customer>));
var context = new InputFormatterContext(
httpContext,
modelName: string.Empty,
modelState: modelState,
metadata: metadata);
// Act
var result = await formatter.ReadAsync(context);
// Assert
Assert.False(result.HasError);
var patchDoc = Assert.IsType<JsonPatchDocument<Customer>>(result.Model);
Assert.Equal("add", patchDoc.Operations[0].op);
Assert.Equal("Customer/Name", patchDoc.Operations[0].path);
Assert.Equal("John", patchDoc.Operations[0].value);
Assert.Equal("remove", patchDoc.Operations[1].op);
Assert.Equal("Customer/Name", patchDoc.Operations[1].path);
}
示例3: 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);
}
示例4: 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);
}
示例5: 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);
}
示例6: ConstructorWithOneParameterInitalizesMembers
public void ConstructorWithOneParameterInitalizesMembers()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
// Act
var viewData = new ViewDataDictionary(metadataProvider);
// Assert
Assert.NotNull(viewData.ModelState);
Assert.NotNull(viewData.TemplateInfo);
Assert.Null(viewData.Model);
Assert.NotNull(viewData.ModelMetadata);
Assert.Equal(0, viewData.Count);
}
示例7: FromStringExpression_SetsContainerAsExpected
public void FromStringExpression_SetsContainerAsExpected()
{
// Arrange
var myModel = new TestModel { SelectedCategory = new Category() };
var provider = new EmptyModelMetadataProvider();
var viewData = new ViewDataDictionary<TestModel>(provider);
viewData["Object"] = myModel;
// Act
var metadata = ExpressionMetadataProvider.FromStringExpression("Object.SelectedCategory",
viewData,
provider);
// Assert
Assert.Same(myModel, metadata.Container.Model);
}
示例8: Constructor_InitalizesMembers
public void Constructor_InitalizesMembers()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var modelState = new ModelStateDictionary();
// Act
var viewData = new ViewDataDictionary<string>(metadataProvider, modelState);
// Assert
Assert.Same(modelState, viewData.ModelState);
Assert.NotNull(viewData.TemplateInfo);
Assert.Null(viewData.Model);
Assert.NotNull(viewData.ModelMetadata);
Assert.Equal(0, viewData.Count);
}
示例9: ObjectTemplateDisplaysNullDisplayTextWhenObjectIsNull
public void ObjectTemplateDisplaysNullDisplayTextWhenObjectIsNull()
{
// Arrange
var html = DefaultTemplatesUtilities.GetHtmlHelper();
var metadata =
new EmptyModelMetadataProvider()
.GetMetadataForType(null, typeof(DefaultTemplatesUtilities.ObjectTemplateModel));
metadata.NullDisplayText = "(null value)";
html.ViewData.ModelMetadata = metadata;
// Act
var result = DefaultDisplayTemplates.ObjectTemplate(html);
// Assert
Assert.Equal(metadata.NullDisplayText, result);
}
示例10: 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);
}
示例11: FromLambaExpression_SetsContainerAsExpected
public void FromLambaExpression_SetsContainerAsExpected()
{
// Arrange
var myModel = new TestModel { SelectedCategory = new Category() };
var provider = new EmptyModelMetadataProvider();
var viewData = new ViewDataDictionary<TestModel>(provider);
viewData.Model = myModel;
// Act
var metadata = ExpressionMetadataProvider.FromLambdaExpression<TestModel, Category>(
model => model.SelectedCategory,
viewData,
provider);
// Assert
Assert.Same(myModel, metadata.Container.Model);
}
示例12: JsonFormatterReadsSimpleTypes
public async Task JsonFormatterReadsSimpleTypes(string content, Type type, object expected)
{
// Arrange
var formatter = new JsonInputFormatter();
var contentBytes = Encoding.UTF8.GetBytes(content);
var httpContext = GetHttpContext(contentBytes);
var modelState = new ModelStateDictionary();
var metadata = new EmptyModelMetadataProvider().GetMetadataForType(null, type);
var context = new InputFormatterContext(httpContext, metadata, modelState);
// Act
await formatter.ReadAsync(context);
// Assert
Assert.Equal(expected, context.Model);
}
示例13: GetBindingContext
private static ModelBindingContext GetBindingContext(Type modelType)
{
var metadataProvider = new EmptyModelMetadataProvider();
ModelBindingContext bindingContext = new ModelBindingContext
{
ModelMetadata = metadataProvider.GetMetadataForType(modelType),
ModelName = "someName",
OperationBindingContext = new OperationBindingContext
{
HttpContext = new DefaultHttpContext(),
MetadataProvider = metadataProvider,
}
};
bindingContext.OperationBindingContext.HttpContext.Request.Method = "GET";
return bindingContext;
}
示例14: ObjectTemplateDisplaysSimpleDisplayTextWhenTemplateDepthGreaterThanOne
public void ObjectTemplateDisplaysSimpleDisplayTextWhenTemplateDepthGreaterThanOne()
{
// Arrange
var model = new DefaultTemplatesUtilities.ObjectTemplateModel();
var html = DefaultTemplatesUtilities.GetHtmlHelper(model);
var metadata =
new EmptyModelMetadataProvider()
.GetMetadataForType(() => model, typeof(DefaultTemplatesUtilities.ObjectTemplateModel));
metadata.SimpleDisplayText = "Simple Display Text";
html.ViewData.ModelMetadata = metadata;
html.ViewData.TemplateInfo.AddVisited("foo");
html.ViewData.TemplateInfo.AddVisited("bar");
// Act
var result = DefaultDisplayTemplates.ObjectTemplate(html);
// Assert
Assert.Equal(metadata.SimpleDisplayText, result);
}
示例15: JsonFormatterReadsComplexTypes
public async Task JsonFormatterReadsComplexTypes()
{
// Arrange
var content = "{name: 'Person Name', Age: '30'}";
var formatter = new JsonInputFormatter();
var contentBytes = Encoding.UTF8.GetBytes(content);
var httpContext = GetHttpContext(contentBytes);
var modelState = new ModelStateDictionary();
var metadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(User));
var context = new InputFormatterContext(httpContext, metadata, modelState);
// Act
await formatter.ReadAsync(context);
// Assert
var model = Assert.IsType<User>(context.Model);
Assert.Equal("Person Name", model.Name);
Assert.Equal(30, model.Age);
}