简体   繁体   English

带有jQuery的MVC4剃须刀进行验证

[英]MVC4 razor with jQuery for validation

I typed up so code to help with validation for my view in C# MVC 4, and it works: 我输入了这样的代码来帮助验证C#MVC 4中的视图,并且它可以正常工作:

jQuery Code: jQuery代码:

 $("#CountryDDL").change(function () {
 if ($("#CountryDDL").val() == 'CA' || $("#CountryDDL").val() == 'US') {

    $("#submitButton").attr("onclick", "return false;");
 }
 else if ($("#CountryDDL").val() != 'CA' || $("#CountryDDL").val() != 'US') {
     $("#submitButton").removeAttr("onclick");
 }
});
 $("#ProvinceDDL, #StateDDL").change(function () {

 if ($("#ProvinceDDL").val() != 'NULL' || $("#StateDDL").val() != 'NULL') {
    $("#submitButton").removeAttr("onclick");

 }
 else if ($("#ProvinceDDL").val() == 'NULL' || $("#StateDDL").val() == 'NULL') {
     $("#submitButton").attr("onclick", "return false;");
 }
 });

What The jQuery Does : jQuery的作用

This jQuery is to help validate two fields ultimately (three technically). 这个jQuery可以帮助最终验证两个字段(技术上为三个)。 If The user selects Canada or United States they are forced to select a province or state otherwise they cannot submit. 如果用户选择加拿大或美国,则他们将被迫选择一个省或州,否则他们将无法提交。

The Problem: 问题:

However, since I have onclick to return false it doesn't let the validation for the other fields to go through on this page. 但是,由于我具有onclick返回false的功能,因此不允许该页面上的其他字段通过验证。

Is there a way to let C# run the validation for the other fields and run my jQuery validation at the same time on click? 有没有一种方法可以让C#在单击时同时运行其他字段的验证并运行我的jQuery验证?

for conditional validation like you mention I use foolproof. 对于像您提到的条件验证,我使用万无一失。 see here Conditional Validation in asp.net MVC4 在此处查看asp.net MVC4中的条件验证

then in jquery you can do 然后在jQuery中,你可以做

$('.btnSubmit').on('click', function(e){
    if($('form').valid()){
    }else{
        e.preventDefault();
        var $validate = $('form').validate();
    }
});

so it will fire the validation and if the form isn't valid then prevent default will stop the submit from happening. 因此它将触发验证,如果表单无效,则阻止默认操作将阻止提交。 Hopefully this helps 希望这会有所帮助

A word of warning you are implementing only client side validation, i hope you have corresponding server side validation. 提醒您您仅实现客户端验证,我希望您具有相应的服务器端验证。

The correct approach to your problem is to impalement MVC validators for both server and client side so you can annotate your model. 解决问题的正确方法是在服务器端和客户端插入MVC验证器,以便您可以注释模型。 Here is an example, adding validators to your model will correctly tie in with the unobtrusive MVC validation and you will not have write javascript with hard coded values. 这是一个示例,将验证器添加到模型中将正确地与不干扰MVC验证相结合,并且您将不会编写带有硬编码值的javascript。 There are many validators to help you out such as [Required] and google for others. 有许多验证器可以帮助您,例如[Required]和google的其他验证器。

Example code is taken from and another link to help you out. 示例代码摘自,另一个链接为您提供帮助。

http://www.asp.net/mvc/tutorials/hands-on-labs/aspnet-mvc-4-helpers,-forms-and-validation http://www.asp.net/mvc/tutorials/hands-on-labs/aspnet-mvc-4-helpers,-forms-and-validation

http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-validation-to-the-model http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-validation-to-the-model

public class Album
     {  
          [Required(ErrorMessage = "An Album Title is required")]
          [DisplayFormat(ConvertEmptyStringToNull = false)]
          [StringLength(160, MinimumLength = 2)]
          public string Title { get; set; }

          [Required(ErrorMessage = "Price is required")]
          [Range(0.01, 100.00, ErrorMessage = "Price must be between 0.01 and 100.00")]
          [DataType(DataType.Currency)]
          public decimal Price { get; set; }

          [DisplayName("Album Art URL")]
          [DataType(DataType.ImageUrl)]
          [StringLength(1024)]
          public string AlbumArtUrl { get; set; }
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM