0

I have used Data Annonation in my project to validate my modals. e.g I have a class with following Data Annonation properties

 public class tbl_store
 {
    [Required(ErrorMessage = "Name is required")]
    public string Name{ get; set; }
    [Required(ErrorMessage = "Email is required")]
    [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Please enter a valid e-mail adress")]
    public string Email{ get; set; }
    [Required(ErrorMessage = "Phone Number is required")]
    [DataType(DataType.PhoneNumber, ErrorMessage = "Invalid Phone Number")]
    [RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Invalid Phone number")]
    public string Contact{ get; set; }
    [Required(ErrorMessage = "Address is required")]
    public string Address{ get; set; }
    [Required(ErrorMessage = "Detail is required")]
    public string Detail{ get; set; }
}

Now i have a form that use the above class as Model and have input textboxes for Name , Email and Contact fields.

When i submit this form, i am using following ActionResult method

  [HttpPost]
    public ActionResult createStore(tbl_store modal)
    {
        try
        {
            using (joyryde_storeEntities context = new joyryde_storeEntities())
            {

                    tbl_store objStore = new tbl_store()
                    {
                        Name = modal.Name,
                        Email = modal.Email,
                        Contact = modal.Contact                          
                    };
                    context.tbl_store.Add(objStore);
                }                   
                context.SaveChanges();
            }
        }
        catch (DbEntityValidationException ex)
        {

        }
       // Some more code
    }  

Here at Context.SaveChanges() exception throws with message System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

When i debug the expcetion, it throws me exception due to Address and Detail fields are required fields and are posted with null values.

I don't wanna add this fields in this form.

How can i nullify Data Annonation for these two fields or some things else to post this form data successfully.

3
  • 2
    Then, as always, use view models containing only the properties you want in the view(s) and map to and from your data model. Commented Mar 8, 2016 at 7:17
  • I have another form, that uses the same data modal class to submit Address and Details later for the same store. Commented Mar 8, 2016 at 7:18
  • 1
    Which is why you use view models! Commented Mar 8, 2016 at 7:22

2 Answers 2

1

You must use view model. View models are some model for creating models that has diffrence property and attributes from the main model. create viewmodel directory in model directory and add your new model that address and details are not required.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem here is not your use/not use of view models. The problem is your database. I assume you are using EF Code First. You have to remove the Required Attributes from your entity to be able to save it to the database without the error you are facing now. If you are not using EF Code First, in addition to removing the attributes, you have to change your database to allow NULL-Values in the 2 columns.

However if you want another view where these 2 fields are required, you have to use 2 view models which will do individual check for each view. One for Name, Email and Contact and another one for Address and Detail.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.