如何禁用特定行Asp.Net MVC的验证 [英] How to disable validation for spesific row Asp.Net MVC

查看:95
本文介绍了如何禁用特定行Asp.Net MVC的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Entity Framework使用ASP.NET MVC编写项目。我有一个acontroller有一个动作(保存动作,提交更改数据库)。



我想在不使用DataAnnotations的情况下绕过验证的Answer属性。在View中,我尝试了一些代码,但我失败了。由于我的动态建模结构,我无法使用DataAnnotations。



那么如何禁用特定属性并使此属性不再通过验证?



这是我的模特:



 [表(答案)] 
公共课回答
{
[Key]
public int AnswerId {get;组; }
public int QuestionId {get;组; }
public int CompanyId {get;组; }


[必需(ErrorMessage =***)]
public string答案{get;组; }

}



必须退出验证的财产。

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

解决方案

亲爱的Fatihx,





只需删除 [必填。 。 上面的行公共字符串答案





您的客户端验证将会消失。



如果,这是您的解决方案,您正在寻找然后选择作为答案。



谢谢。

Manoj Kalla


亲爱的Fatihx,





其他你必须用GET / POST方法写的页面,根据你的情况检查。

ModelState.Remove(答案)



肯定会有效。



谢谢。

Manoj Kalla


< blockquote>理想的方法是使用viewmodel

例如:



  public  答案 
{
[Key]
public int AnswerId {get;组; }
public int QuestionId {get;组; }
public int CompanyId {get;组; }


[必需(ErrorMessage = ***)]
public string 答案 {get;组; }
}

public class AnswerViewModel
{
[Key]
public int AnswerId {get;组; }
public int QuestionId {get;组; }
public int CompanyId {get;组; }



public string 答案 {get;组; }
}









n使用AnswerViewModel作为第二页


I code a project with ASP.NET MVC using Entity Framework. I have acontroller which has one action(Save action which commit changes to database).

I want to bypass "Answer" property out of validation without using DataAnnotations. In View, i tried some code but i failed. Because of my dynamic modelling structure i cant use DataAnnotations.

So how can i disable a spesific 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 }) 

解决方案

Dear Fatihx,


Just remove a [Required . . line from above public string Answer


Your clientside validation would be goes off.

If, this is your solution, you are looking for then select as answer.

Thank You.
Manoj Kalla


Dear Fatihx,


For other page you have to write in GET/POST method, check as per your situation.
ModelState.Remove("Answer")

sure, it will work.

Thank You.
Manoj Kalla


the ideal way is to use viewmodel
eg:

public Answer
{
    [Key]
        public int AnswerId { get; set; }
        public int QuestionId { get; set; }
        public int CompanyId { get; set; }
 

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

public class AnswerViewModel
{
   [Key]
        public int AnswerId { get; set; }
        public int QuestionId { get; set; }
        public int CompanyId { get; set; }
 

        
        public string Answer { get; set; }
}





n use AnswerViewModel for second page


这篇关于如何禁用特定行Asp.Net MVC的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆