I have a view model
public class GeneralPageVM
{
[Required]
public string Reference { get; set; }
[Attributes.BasicSelectionListMandatory(ErrorMessage = "Please select at least one supplier")]
public ViewModels.Shared.BasicSelectionListVM Suppliers { get; set; }
}
It has a property that is another view model
public class BasicSelectionListVM
{
public ListItemList { get; set; }
}
I add the parent view model to a view like this
@model ViewModels.RFQ.GeneralPageVM
....
@Html.ValidationMessageFor(x => x.Suppliers)
@Html.EditorFor(x => x.Suppliers, "_BasicSelectionList")
And the child like this
@model ViewModels.Shared.BasicSelectionListVM
@Html.HiddenFor(m => Model.ItemList[i].Id) @Html.DisplayFor(m => Model.ItemList[i].Description) | @Html.CheckBoxFor(m => Model.ItemList[i].IsChecked) |
I have created a custom attribute
sealed public class BasicSelectionListMandatoryAttribute : ValidationAttribute, IClientValidatable
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var basicSelectionListVM = (ViewModels.Shared.BasicSelectionListVM)value;
if (!basicSelectionListVM.ItemList.Where(x => (x.IsChecked == true)).Any())
return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
else
return null;
}
public IEnumerableGetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
yield return new ModelClientValidationRule
{
ErrorMessage = this.ErrorMessage,
ValidationType = "basicselectionlistmandatory"
};
}
}
And am trying to get it to execute these as validation functions
$(function () {
jQuery.validator.addMethod("basicselectionlisthasselection", function (value, element, params) {
return true;
}, '');
jQuery.validator.unobtrusive.adapters.add("basicselectionlistmandatory", {}, function (options) {
options.rules["basicselectionlisthasselection"] = true;
options.messages["basicselectionlisthasselection"] = options.message;
});
})
The 2 jQuery functions are successfully added when I inspect jQuery.validator
And it puts this is in the page as a place for the validation to occur.
But nothing fires when I test it? I read online you can only validate input and selects... but then why does it add a span tag?
It clearly hasn't added the validation attribute.
Can anyone please suggest why this isn't working?
Thanks
Hi,
So far as I know, if we want to create a custom validation, we must do the two parts:
- Creating a custom validation attribute. This attribute enables you to create custom metadata that you use in the data model for validation.
Applying the custom validation attribute. After the custom attribute is created, you apply it to the data fields that you want to validate.
There are some articals about custom validation, please refer to the links below:
http://msdn.microsoft.com/en-us/library/vstudio/cc668224(v=vs.100).aspx
http://www.codeproject.com/Articles/260177/Custom-Validation-Attribute-in-ASP-NET-MVC
Hope it's useful for you.
Best Regards,
Michelle Ge
沒有留言:
張貼留言