3

I have an ASP.NET MVC 3 project that uses .Net 4.0 Framework and Entity Framework 4.4. I am using the code first w/ migrations approach for the Entity Framework.

I have an object A that has a property of object B. object A really just needs to know the id for object B, but MVC is enforcing all annotation validations on object B as I've marked it required in object A (which it is). ModelState.IsValid is always returning false because some validations are failing on object B when the form is submitted.

Example:

    public class FormField
    {
        public int Id { get; set; }

        [MaxLength(50)]
        [Required]
        [DisplayName("Field Name")]
        public string Name { get; set; }

        [Required]
        public Form Form { get; set; }
    }

    public class Form
    {
        public Form()
        {
            Fields = new List<FormField>();
        }

        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        public List<FormField> Fields { get; set; }
    }   

This isn't a problem when editing an existing FormField as I can just put a hidden field with the Form.Name property on the page (this still strikes me as something that should be unnecessary). The issue arises when creating a new FormField. I display a drop down list of forms (this is populated from my view model), and make that field point to the FormField.Form.Id property. ASP .NET MVC is still expecting formField.Form.Name (as this was marked as required on the Form object).

If I remove the "[Required]" annotation from the Form field of the FormField object, the validations wouldn't fire, but this would make the foreign key to Form.Id nullable in the database, which it shouldn't be.

Any thoughts? I'm probably doing something wrong here but I'm not entirely sure what.

jason
  • 2,219
  • 5
  • 33
  • 66
  • Take a look at the anwser of Doku-so on http://stackoverflow.com/questions/3642371/how-to-update-only-one-field-using-entity-framework. There is a ValidateOnSaveEnabled option (http://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbcontextconfiguration.validateonsaveenabled(v=vs.113).aspx) – Martijn van Put Jul 22 '14 at 20:48
  • @MartijnvanPut I'm still working through Doku-so's answer and thinking about it. I will say that setting the ValidateOnSaveEnabled property to false isn't an option. On the whole, the annotation validations will be correct. This example is kind of a corner case for some of the entities though. – jason Jul 22 '14 at 21:36

2 Answers2

1

I removed the [Required] annotation on FormField.Form. That didn't fix the issue. I tried adding "[Bind(Exclude = "vm.FormField.Form")]" to the parameter being posted. This stops the binding, but ensures I'm still left with the validation errors in ModelState.

Ultimately, I had to do:

        ModelState.Remove("FormField.Form.Id");
        ModelState.Remove("FormField.Form.Name");

And this prevented the ModelState errors I was getting. As I really only need the Form.Id, I had a property in my ViewModel "SelectedForm" and use this property for that value.

So...this works, but...this seems a pretty tedious solution as if I had more required fields on the FormField.Form object, I would have to list each one as well as hard code the property name in a string form.

Can anyone think of a way to refine this approach?

jason
  • 2,219
  • 5
  • 33
  • 66
0

Maybe you could remove the [Required] attribute and instead use the EF fluent API to configure this:

modelBuilder.Entity<FormField>().Property(ff => ff.Form).IsRequired();
Pragmateek
  • 13,174
  • 9
  • 74
  • 108