如何使自定义DropDownList编辑器模板的Required属性在客户端运行?

时间:2022-11-27 07:40:16

I have an editor template for DropDownLists that is marked with an attribute like this:

我有一个DropDownLists的编辑器模板,标有这样的属性:

[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.

我的问题是它只验证服务器端,这对于用户解决所有客户端验证非常烦人,只是提交并获得新的,意外的服务器端验证。

2 个解决方案

#1


1  

If your client side validation doesn't work it may be caused by one of the following reasons:

如果您的客户端验证不起作用,可能是由以下原因之一引起的:

  1. Your web.config doesn't have that enteries:

    你的web.config没有那个企业:

    &ltappSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    </appSettings>
    
  2. 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>
    
  3. Your controls are not surrounded by Html.BeginForm or Ajax.BeginForm

    您的控件未被Html.BeginForm或Ajax.BeginForm包围

  4. Client-side validation can stop working in EditorFor after update to ASP.NET MVC 4 if you use:

    如果您使用以下内容,客户端验证可能会在更新到ASP.NET MVC 4后停止在EditorFor中工作:

    @Html.DropDownListFor(m => Model, listWithSelected, " - select - ")
    

    Replacing Model with m should resolve problem:

    用m替换模型应解决问题:

    @Html.DropDownListFor(m => m, listWithSelected, " - select - ")
    

#2


0  

Set the required class

设置所需的类

@Html.DropDownListFor(m => Model, listWithSelected, " - select - ", new { @class='required' })

See ASP MVC 3: How can I do Client side Validation on a Select List?

请参阅ASP MVC 3:如何在选择列表上进行客户端验证?

#1


1  

If your client side validation doesn't work it may be caused by one of the following reasons:

如果您的客户端验证不起作用,可能是由以下原因之一引起的:

  1. Your web.config doesn't have that enteries:

    你的web.config没有那个企业:

    &ltappSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    </appSettings>
    
  2. 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>
    
  3. Your controls are not surrounded by Html.BeginForm or Ajax.BeginForm

    您的控件未被Html.BeginForm或Ajax.BeginForm包围

  4. Client-side validation can stop working in EditorFor after update to ASP.NET MVC 4 if you use:

    如果您使用以下内容,客户端验证可能会在更新到ASP.NET MVC 4后停止在EditorFor中工作:

    @Html.DropDownListFor(m => Model, listWithSelected, " - select - ")
    

    Replacing Model with m should resolve problem:

    用m替换模型应解决问题:

    @Html.DropDownListFor(m => m, listWithSelected, " - select - ")
    

#2


0  

Set the required class

设置所需的类

@Html.DropDownListFor(m => Model, listWithSelected, " - select - ", new { @class='required' })

See ASP MVC 3: How can I do Client side Validation on a Select List?

请参阅ASP MVC 3:如何在选择列表上进行客户端验证?