简体   繁体   English

如何在 asp.net mvc 中创建 customValidation?

[英]How to create a customValidation in asp.net mvc?

I have form and it does not validate the error message on View.我有表单,但它不会验证 View 上的错误消息。 When i inspect it there no errors at all.当我检查它时,根本没有错误。 I need some help here below to improve my logic better.我在下面需要一些帮助来更好地改进我的逻辑。 What i want exactly when leaving the EditorFor, there should be an error states "This field is required" with an image caption(error-image).离开EditorFor时我到底想要什么,应该有一个带有图像标题(错误图像)的错误状态“此字段是必需的”。

// View
<div class="form-group row">
<label for="Attendee" class="col-sm-2 col-form-label">Attendee Cell Number*</label>
                            <div class="col-sm-3">
                                @Html.EditorFor(model => model.RegForm.CellNumber, new { htmlAttributes = new { @class = "form-control", autofocus = "autofocus", placeholder = "Cell Number" } })
                                @Html.ValidationMessageFor(model => model.RegForm.CellNumber)
                            </div>
                        </div>

// Model
    public class RegistrationTrainingForm
    {
       
        public string Title { get; set; }
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string Position { get; set; }

        public string Company { get; set; }

        public string StreetAddress {get ; set;}

        public string StreetAddressLine { get; set; }

        public string City { get; set; }

        public string State { get; set; }

        public string Country { get; set; }

        public int ZipCode { get; set; }

        public string Email { get; set; }

        [Required(ErrorMessage = "This field is required")]
        
        [DataType(DataType.PhoneNumber)]
        
        public string CellNumber { get; set; }

        public string DietaryRequirements { get; set;}

        public int Country_Id { get; set; }

        public string Country_Name { get; set; }
    }

//CustomValidation.cs
  public class CustomFormValidation:ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            
                
            return base.IsValid(value, validationContext);
        }
    }

You are talking about client side validation.您正在谈论客户端验证。 Can you make sure followings are ok:您能否确保以下内容正常:

#1. #1。 Ensure you have the following files loading on your page (the files could be of different versions):确保您的页面上加载了以下文件(这些文件可能具有不同的版本):

<script src="~/Scripts/jquery.js"></script>    
<script src="~/Scripts/jquery.validation.js"></script>    
<script src="~/Scripts/jquery.validation.unobtrusive.js"></script>

#2. #2。 In your web.config file you have the following sections:在您的 web.config 文件中,您有以下部分:

<appSettings>
   <add key="ClientValidationEnabled" value="true" />
   <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

#3. #3。 When the page is rendered check from the view source that in the input control you have the data-val="true" property set.呈现页面时,从视图源检查输入控件中是否设置了data-val="true"属性。

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

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