简体   繁体   English

如何禁用特定行Asp.Net MVC的验证

[英]How to disable validation for specific row Asp.Net MVC

I code a project with ASP.NET MVC using Entity Framework. 我使用Entity Framework使用ASP.NET MVC编写项目。 I have a controller which has one action(Save action which commit changes to database). 我有一个控制器,它具有一个动作(保存将更改提交到数据库的动作)。

I want to bypass "Answer" property out of validation without using DataAnnotations. 我想在不使用DataAnnotations的情况下绕过“ Answer”属性进行验证。 In View, i tried some code but failed. 在View中,我尝试了一些代码,但失败了。 Because of my dynamic modelling structure i cant use DataAnnotations. 由于我的动态建模结构,我无法使用DataAnnotations。

So how can i disable a specific property and make this property out of validation? 那么,如何禁用特定属性并使该属性无法通过验证?

Here is my model: 这是我的模型:

  [Table("Answers")]
    public class Answer
    {
        [Key]
        public int AnswerId { get; set; }
        public int QuestionId { get; set; }
        public int CompanyId { get; set; }


        [Required(ErrorMessage = "***")]
        public string Answer { get; set; }

    }

The property which must be out of validation. 必须取消验证的属性。

@Html.TextAreaFor(mo => mo.Answers[j].Answer, new { style = "width: 100%", data_val = false })

You can simply disable client side validation in the Razor view prior to render the field and re-enable client side validation after the field has been rendered. 您可以在呈现字段之前先在Razor视图中禁用客户端验证,然后在呈现字段后重新启用客户端验证。

This will work :- 这将起作用:-

@{ Html.EnableClientValidation(false); }
@Html.TextAreaFor(mo => mo.Answers[j].Answer, new { style = "width: 100%", data_val = false })
@{ Html.EnableClientValidation(true); }

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

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