Reputation: 4209
I have the following property in a ViewModel:
[UIHint("AccessDropDown")]
[Display(Name ="Access Level")]
[Range(1, 2, ErrorMessage = "Required.")]
public int InternalUserAccess { get; set; }
AccessDropDown is a PartialView
which lives in the Views folder, under shared, under EditorTemplates directory. It is a KendoDropDownList
like this:
@(Html.Kendo().DropDownList().Name("UserAccess").OptionLabel("Select")
.BindTo(new List<DropDownListItem>
{
new DropDownListItem{Text = "User", Value = "1"},
new DropDownListItem{Text = "Admin", Value = "2"}
}
))
The problem is that despite me adding a [Required]
annotation on the above property and despite the Range annotation which I also added to solve this; it still doesn't validate - a value of zero is set to the option label instead!
I have tried removing the OptionLabel
and adding a new item to the list with a value outside the set range such as:
new DropDownListItem{Text = "Select", Value = "1564"},
But this still didn't validate and the value of 1564 went into the controller to be entered to the database!
Any ideas why a PartialView
with a UIHINT
wont 'obey' the validation annotation on the view model?
Please let us know..
Many thanks.
Upvotes: 2
Views: 1035
Reputation: 20233
Does it work with a regular TextBox Html helper, do you see the validation then? If it does then I would assume that the only problem is that you are using different Name for the widget.
Try to set the name to be either .Name("InternalUserAccess")
or make it generic so it becomes the same as the name of the property that you are editing with that editor template
e.g.
@model int
@(Html.Kendo().DropDownListFor(m=>m).OptionLabel("Select")
.BindTo(new List<DropDownListItem>
{
new DropDownListItem{Text = "User", Value = "1"},
new DropDownListItem{Text = "Admin", Value = "2"}
}
))
Upvotes: 0