当前位置: 首页>>代码示例>>C#>>正文


C# ModelBinding.EmptyModelMetadataProvider类代码示例

本文整理汇总了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);
        }
开发者ID:njannink,项目名称:sonarlint-vs,代码行数:27,代码来源:ModelBindingHelperTest.cs

示例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);
        }
开发者ID:huoxudong125,项目名称:Mvc,代码行数:30,代码来源:JsonPatchInputFormatterTest.cs

示例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);
        }
开发者ID:njannink,项目名称:sonarlint-vs,代码行数:12,代码来源:ModelExplorerTest.cs

示例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);
        }
开发者ID:njannink,项目名称:sonarlint-vs,代码行数:12,代码来源:ModelExplorerTest.cs

示例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);
        }
开发者ID:huoxudong125,项目名称:Mvc,代码行数:15,代码来源:ModelExplorerExtensionsTest.cs

示例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);
        }
开发者ID:RehanSaeed,项目名称:Mvc,代码行数:15,代码来源:ViewDataDictionaryTest.cs

示例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);
        }
开发者ID:huoxudong125,项目名称:Mvc,代码行数:16,代码来源:ExpressionMetadataProviderTest.cs

示例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);
        }
开发者ID:njannink,项目名称:sonarlint-vs,代码行数:16,代码来源:ViewDataDictionaryOfTModelTest.cs

示例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);
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:16,代码来源:DefaultDisplayTemplatesTests.cs

示例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);
        }
开发者ID:njannink,项目名称:sonarlint-vs,代码行数:17,代码来源:ModelExplorerTest.cs

示例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);
        }
开发者ID:huoxudong125,项目名称:Mvc,代码行数:17,代码来源:ExpressionMetadataProviderTest.cs

示例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);
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:17,代码来源:JsonInputFormatterTest.cs

示例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;
        }
开发者ID:ryanbrandenburg,项目名称:Mvc,代码行数:18,代码来源:HttpRequestMessageModelBinderTest.cs

示例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);
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:19,代码来源:DefaultDisplayTemplatesTests.cs

示例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);
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:20,代码来源:JsonInputFormatterTest.cs


注:本文中的Microsoft.AspNet.Mvc.ModelBinding.EmptyModelMetadataProvider类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。