我有一个DropDownLists的编辑器模板,该模板用这样的属性标记:
[AttributeUsage(AttributeTargets.Property)]
public class DropDownListAttribute : UIHintAttribute
{
public string SelectListName { get; set; }
public DropDownListAttribute(string selectListName)
: base("DropDownList", "MVC", selectListName)
{
SelectListName = selectListName;
}
}
它本身看起来像这样:
@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 - ")
我的问题是它仅验证服务器端,这对于用户解决所有客户端验证,仅提交并获得新的令人惊讶的服务器端验证非常烦人。
最佳答案
如果您的客户端验证不起作用,则可能是由于以下原因之一引起的:
<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 - ")
用
Model
替换m
应该可以解决问题:@Html.DropDownListFor(m => m, listWithSelected, " - select - ")
关于asp.net-mvc - 如何使我的自定义DropDownList编辑器模板的Required属性在客户端运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14339445/