I have an editor template for DropDownLists that is marked with an attribute like this:
[AttributeUsage(AttributeTargets.Property)]
public class DropDownListAttribute : UIHintAttribute
{
public string SelectListName { get; set; }
public DropDownListAttribute(string selectListName)
: base("DropDownList", "MVC", selectListName)
{
SelectListName = selectListName;
}
}
And itself looks like this:
@using Comair.RI.UI.Core
@{
var list = this.GetModelSelectList();
var listWithSelected = new SelectList(list.Items, list.DataValueField, list.DataTextField, Model);
}
@Html.DropDownListFor(m => Model, listWithSelected, " - select - ")
My issue here is it only validates server side, which is very annoying for a user to resolve all client side validations, only to submit and get a new, surprise server side validation.
If your client side validation doesn't work it may be caused by one of the following reasons:
Your web.config doesn't have that enteries:
<appSettings> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings>
You forgotten to add validation scripts:
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
Your controls are not surrounded by Html.BeginForm
or Ajax.BeginForm
Client-side validation can stop working in EditorFor after update to ASP.NET MVC 4 if you use:
@Html.DropDownListFor(m => Model, listWithSelected, " - select - ")
Replacing Model
with m
should resolve problem:
@Html.DropDownListFor(m => m, listWithSelected, " - select - ")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With