Merge branch 'release' into dev
This commit is contained in:
commit
0ce5aed8dc
|
|
@ -190,11 +190,9 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||||
// If the Model at the current node is null and there is no parent, we cannot validate, and the
|
// If the Model at the current node is null and there is no parent, we cannot validate, and the
|
||||||
// DataAnnotationsModelValidator will throw. So we intercept here to provide a catch-all value-required
|
// DataAnnotationsModelValidator will throw. So we intercept here to provide a catch-all value-required
|
||||||
// validation error
|
// validation error
|
||||||
var modelStateKey = ModelBindingHelper.CreatePropertyModelName(ModelStateKey,
|
|
||||||
ModelMetadata.GetDisplayName());
|
|
||||||
if (parentNode == null && ModelMetadata.Model == null)
|
if (parentNode == null && ModelMetadata.Model == null)
|
||||||
{
|
{
|
||||||
modelState.TryAddModelError(modelStateKey, Resources.Validation_ValueNotFound);
|
modelState.TryAddModelError(ModelStateKey, Resources.Validation_ValueNotFound);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -96,10 +96,9 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
|
|
||||||
Assert.Null(result.HeaderValue);
|
Assert.Null(result.HeaderValue);
|
||||||
Assert.Null(result.HeaderValues);
|
Assert.Null(result.HeaderValues);
|
||||||
|
|
||||||
// This is a bug - the model state error key is wrong here.
|
|
||||||
var error = Assert.Single(result.ModelStateErrors);
|
var error = Assert.Single(result.ModelStateErrors);
|
||||||
Assert.Equal("transactionId.transactionId", error);
|
Assert.Equal("transactionId", error);
|
||||||
}
|
}
|
||||||
|
|
||||||
// The action that this test hits will echo back the model-bound values
|
// The action that this test hits will echo back the model-bound values
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
#if ASPNET50
|
#if ASPNET50
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
@ -245,6 +246,27 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||||
Assert.Empty(log);
|
Assert.Empty(log);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Validate_ValidatesIfModelIsNull()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var modelMetadata = GetModelMetadata(typeof(ValidateAllPropertiesModel));
|
||||||
|
var node = new ModelValidationNode(modelMetadata, "theKey");
|
||||||
|
|
||||||
|
var context = CreateContext(modelMetadata);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
node.Validate(context);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var modelState = Assert.Single(context.ModelState);
|
||||||
|
Assert.Equal("theKey", modelState.Key);
|
||||||
|
Assert.Equal(ModelValidationState.Invalid, modelState.Value.ValidationState);
|
||||||
|
|
||||||
|
var error = Assert.Single(modelState.Value.Errors);
|
||||||
|
Assert.Equal("A value is required but was not present in the request.", error.ErrorMessage);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
[ReplaceCulture]
|
[ReplaceCulture]
|
||||||
public void Validate_ValidateAllProperties_AddsValidationErrors()
|
public void Validate_ValidateAllProperties_AddsValidationErrors()
|
||||||
|
|
@ -321,6 +343,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||||
return new DataAnnotationsModelMetadataProvider().GetMetadataForType(() => o, o.GetType());
|
return new DataAnnotationsModelMetadataProvider().GetMetadataForType(() => o, o.GetType());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static ModelMetadata GetModelMetadata(Type type)
|
||||||
|
{
|
||||||
|
return new DataAnnotationsModelMetadataProvider().GetMetadataForType(modelAccessor: null, modelType: type);
|
||||||
|
}
|
||||||
|
|
||||||
private static ModelValidationContext CreateContext(ModelMetadata metadata = null)
|
private static ModelValidationContext CreateContext(ModelMetadata metadata = null)
|
||||||
{
|
{
|
||||||
var providers = new IModelValidatorProvider[]
|
var providers = new IModelValidatorProvider[]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue