Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何在ASP.NETMVC中使用单选按钮的自定义消息在客户端添加所需的字段验证?_Jquery_Asp.net Mvc - Fatal编程技术网

Jquery 如何在ASP.NETMVC中使用单选按钮的自定义消息在客户端添加所需的字段验证?

Jquery 如何在ASP.NETMVC中使用单选按钮的自定义消息在客户端添加所需的字段验证?,jquery,asp.net-mvc,Jquery,Asp.net Mvc,如何在ASP.NETMVC中使用单选按钮的自定义消息在客户端添加所需的字段验证 <div class="form-group"> <div class="col-sm-4 control-label"> <label class="hthin" asp-for="IsGuidance"></label> <l

如何在ASP.NETMVC中使用单选按钮的自定义消息在客户端添加所需的字段验证

<div class="form-group">
                    <div class="col-sm-4 control-label">
                        <label class="hthin" asp-for="IsGuidance"></label>
                        <label class="hthin">Coach: @Model.GuideName</label>
                </div>
                <div class="col-sm-3 checkbox-inline">
                        @Html.RadioButtonFor(model => model.IsGuidance, 1, new { @Id = "IsGuidanceYes" })
                        <label class="hthin" for="IsGuidanceYes">Yes</label>
                        @Html.RadioButtonFor(model => model.IsGuidance, 0, new { @Id = "IsGuidanceNo" })
                        <label class="hthin" for="IsGuidanceNo">No</label>
                        <span asp-validation-for="IsGuidance" class="text-danger"></span>
                </div>
               </div>

但是,这不是验证(或)验证消息没有显示在UI上。

要在Asp.Net MVC中自动进行客户端验证,您需要遵循某些步骤,即

1-使用DataAnnotationAttribute装饰viewmodel属性,这也是服务器端验证所需的

2-验证客户端验证是否已启用。您可以通过不同的方式参考此视频以启用客户端验证。顺便说一下,默认情况下,它始终处于启用状态:

3-确保页面中按所示顺序引用了这3个jQuery库 下: jquery.js、jquery.validate.js和jquery.validate.unobtrusive.js

4-对表单使用@Html.BeginForm helper方法,并将所有表单字段放在其中。 所有表单字段也应该使用适当的@Html助手方法生成

仅此而已,当您提交表单时,客户端验证应该起作用

对于自定义消息,请使用自定义错误消息装饰数据注释属性,以显示自定义错误消息,如

   class ViewModel
    {
      [Required(ErrorMessage ="Custom error message")]
      public bool IsGuidance{get;set;}
    }
我在中进行了测试,您的客户端验证规则似乎运行良好。您应该做的是使用
errorPlacement
errorElement
errorContainer
validate()函数中放置自定义错误消息:

jQuery(位于
$(document.ready()
)内部

// hint: you can use $('form') selector
// using errorPlacement
$('#form').validate({
    errorPlacement: function (error, element) {
        error.appendTo($('#errorPlaceholderName'));
    }
});

// alternative with errorElement & errorLabelContainer
$('#form').validate({
    errorElement: 'span',
    errorLabelContainer: '.text-danger'
});
您还可以尝试将服务器端viewmodel属性验证属性:

型号

// hint: you can use $('form') selector
// using errorPlacement
$('#form').validate({
    errorPlacement: function (error, element) {
        error.appendTo($('#errorPlaceholderName'));
    }
});

// alternative with errorElement & errorLabelContainer
$('#form').validate({
    errorElement: 'span',
    errorLabelContainer: '.text-danger'
});
[Required(ErrorMessage = "IsGuidance is required")]
public int? IsGuidance { get; set; }
注意:
规则('add',…)
方法必须遵循
validate()
才能启用客户端验证,如中所示


参考:

是否要将
RequiredAttribute
@Html.ValidationMessageFor(model=>model.isguide)
一起使用?如何在客户端使用Required属性?我知道在服务器端针对模型属性使用了Model
RequiredAttribute
上的Required属性。要在客户端使用它,您可以查看jQuery验证。是的,我正在寻找jQuery验证。这个场景完全是客户端验证,您可以通过查看来实现这一点。根据您的实现,给定的步骤本质上是相似的。