Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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 }) 
Posted
Updated 12-Aug-14 1:10am
v2

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
 
Share this answer
 
Comments
Fatihx 12-Aug-14 7:51am    
Thanks for your answer.

I am using this model for another pages.

I just need disable it just for one View page.
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
 
Share this answer
 
v2
the ideal way is to use viewmodel
eg:

CSS
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900