public ActionResult ModelBindingExample2()
 {
     var vm = new ModelBindingViewModel();
     vm.AsList();
     return View("ModelBindingExample", vm);
 }
 public ActionResult ModelBindingExample2(ModelBindingViewModel vm)
 {
     vm.AsList();
     return View("ModelBindingExample", vm);
 }
        public void Successfully_validate_ienumerable_property(int[] submittedValues)
        {
            const string valueProperty = "Id";
            const string nameProperty = "Name";
            const string listProperty = "List";
            var vm = new ModelBindingViewModel { RequiredListIds = submittedValues };
            var validationContext = new ValidationContext(vm, null, null);
            var attribute = new ExistsInAttribute(listProperty, valueProperty, nameProperty);

            var result = attribute.GetValidationResult(vm.RequiredListIds, validationContext);

            Assert.That(result, Is.Null, string.Format("Validation failed with message: {0}", attribute.ErrorMessage));
        }
 public ActionResult ModelBindingExample(ModelBindingViewModel vm)
 {
     return View(vm);
 }
        public void Successfully_validate_an_invalid_submission_if_validation_disabled_globally_and_not_overidden()
        {
            const string valueProperty = "Id";
            const string nameProperty = "Name";
            const string listProperty = "List";
            var vm = new ModelBindingViewModel { RequiredListId = 3 };
            var validationContext = new ValidationContext(vm, null, null);
            ExistsInAttribute.EnableValidation = false;
            var attribute = new ExistsInAttribute(listProperty, valueProperty, nameProperty);

            var result = attribute.GetValidationResult(vm.RequiredListId, validationContext);

            Assert.That(result, Is.EqualTo(ValidationResult.Success));
        }
        public void Successfully_validate_string_property(ModelBindingViewModel vm)
        {
            const string valueProperty = "Id";
            const string nameProperty = "Name";
            const string listProperty = "List";
            var validationContext = new ValidationContext(vm, null, null);
            var attribute = new ExistsInAttribute(listProperty, valueProperty, nameProperty);

            var result = attribute.GetValidationResult(vm.RequiredString, validationContext);

            Assert.That(result, Is.Null, string.Format("Validation failed with message: {0}", attribute.ErrorMessage));
        }
        public void Fail_to_validate_an_invalid_submission_if_validation_disabled_globally_and_enabled_locally()
        {
            const string valueProperty = "Id";
            const string nameProperty = "Name";
            const string listProperty = "List";
            var vm = new ModelBindingViewModel { RequiredListId = 3 };
            var validationContext = new ValidationContext(vm, null, null);
            ExistsInAttribute.EnableValidation = false;
            var attribute = new ExistsInAttribute(listProperty, valueProperty, nameProperty, enableValidation: true);

            var result = attribute.GetValidationResult(vm.RequiredListId, validationContext);

            var expectedError = string.Format("The {{0}} field was 3, but must be one of A, B");
            Assert.That(result.ErrorMessage, Is.EqualTo(string.Format(expectedError, validationContext.DisplayName)));
        }
        public void Fail_validation_if_single_property_value_not_in_list(int submittedValue)
        {
            const string valueProperty = "Id";
            const string nameProperty = "Name";
            const string listProperty = "List";
            var vm = new ModelBindingViewModel { RequiredListId = submittedValue };
            var validationContext = new ValidationContext(vm, null, null) { DisplayName = "Required list ids" };
            var attribute = new ExistsInAttribute(listProperty, valueProperty, nameProperty);

            var result = attribute.GetValidationResult(vm.RequiredListId, validationContext);

            var expectedError = string.Format("The {{0}} field was {0}, but must be one of A, B", string.Join(", ", submittedValue));
            Assert.That(result.ErrorMessage, Is.EqualTo(string.Format(expectedError, validationContext.DisplayName)));
        }
        public void Allow_validation_against_lists_containing_null_values(int?[] submittedValues)
        {
            const string valueProperty = "Id";
            const string nameProperty = "Name";
            const string listProperty = "List";
            var vm = new ModelBindingViewModel { RequiredNullableListIds = submittedValues };
            var validationContext = new ValidationContext(vm, null, null) { DisplayName = "Required list ids" };
            var attribute = new ExistsInAttribute(listProperty, valueProperty, nameProperty);

            var result = attribute.GetValidationResult(vm.RequiredNullableListIds, validationContext);

            var expectedError = string.Format("The {{0}} field was {0}, but must be one of A, B", string.Join(", ", submittedValues.Select(s => s == null ? "null" : s.ToString())));
            Assert.That(result.ErrorMessage, Is.EqualTo(string.Format(expectedError, validationContext.DisplayName)));
        }
 public ActionResult ModelBindingExample2(ModelBindingViewModel vm)
 {
     vm.AsList();
     return(View("ModelBindingExample", vm));
 }
 public ActionResult ModelBindingExample(ModelBindingViewModel vm)
 {
     return(View(vm));
 }