Example #1
0
        public static string GetUserErrorMessageOrDefault(ModelError modelError, ModelStateEntry entry)
        {
            if (!string.IsNullOrEmpty(modelError.ErrorMessage))
            {
                return modelError.ErrorMessage;
            }

            if (entry == null)
            {
                return string.Empty;
            }

            var attemptedValue = entry.AttemptedValue ?? "null";
            return Resources.FormatCommon_ValueNotValidForProperty(attemptedValue);
        }
        public void ProcessDto_RequiredFieldNull_RaisesModelErrorWithMessage()
        {
            // Arrange
            var model             = new Person();
            var containerMetadata = GetMetadataForObject(model);

            var bindingContext = CreateContext(containerMetadata);

            ComplexModelDto dto = new ComplexModelDto(containerMetadata, containerMetadata.Properties);
            TestableMutableObjectModelBinder testableBinder = new TestableMutableObjectModelBinder();

            // Make ValueTypeRequired invalid.
            var propertyMetadata = dto.PropertyMetadata.Single(p => p.PropertyName == "ValueTypeRequired");

            dto.Results[propertyMetadata] =
                new ComplexModelDtoResult(null, new ModelValidationNode(propertyMetadata, "theModel.ValueTypeRequired"));

            // Act
            testableBinder.ProcessDto(bindingContext, dto);

            // Assert
            ModelStateDictionary modelStateDictionary = bindingContext.ModelState;

            Assert.Equal(false, modelStateDictionary.IsValid);
            Assert.Equal(1, modelStateDictionary.Count);

            // Check ValueTypeRequired error.
            ModelState modelState;

            Assert.True(modelStateDictionary.TryGetValue("theModel.ValueTypeRequired", out modelState));
            Assert.Equal(1, modelState.Errors.Count);

            ModelError modelError = modelState.Errors[0];

            Assert.Null(modelError.Exception);
            Assert.NotNull(modelError.ErrorMessage);
            Assert.Equal("Sample message", modelError.ErrorMessage);
        }
 private static void AssertRequiredError(string key, ModelError error)
 {
     // Mono issue - https://github.com/aspnet/External/issues/19
     Assert.Equal(PlatformNormalizer.NormalizeContent(
         string.Format("The {0} field is required.", key)), error.ErrorMessage);
     Assert.Null(error.Exception);
 }
 private static void AssertRequiredError(string key, ModelError error)
 {
     Assert.Equal(string.Format("The {0} field is required.", key), error.ErrorMessage);
     Assert.Null(error.Exception);
 }