Ta01
Ta01

Reputation: 31610

T4 Self Tracking Entities And Model Validation

I'm using EF4.x Self Tracking Entities in my project and am trying to achieve Model Validation in my MVC4 web application, however, my Model State always seems to be Valid. I'm using T4 templates to generate my "buddy" classes. Below is an example of one of the STE's and its buddy:

STE - generated using T4:

[DataContract(IsReference = true)]
[KnownType(typeof(Filing))]
public partial class FilingHistory: IObjectWithChangeTracker, INotifyPropertyChanged
{
   public int FilingHistoryId 
   {
     //Snipped for Brevity
   }
   // Navigation, ChangeTracking, Association Fix up snipped
}

Here is the Buddy Class generated also via a T4 template I wrote:

[MetadataType(typeof(FilingHistoryMetaData))]
public partial class FilingHistory
  {
    // Partial Class 
  }

public class FilingHistoryMetaData
{           
   [Display(Name = "Filing History Id")]
   [Required(ErrorMessage = "Filing History Id is Required.")]
   int FilingHistoryId { get; set; }    
   // Other properties snipped for Brevity
}

I'm going to exclude the key's from each MetaData class because those will be created automatically (just as an fyi). Also, the namespaces for the STE, the empty partial and the buddy class are identical

When I create a simple controller in MVC4 just to test it out, with a Create Template, on the HttpPost Action of Create I have some code as shown below:

[HttpPost]
public ActionResult Create(FilingHistory filingHistoryToCreate)
 {
    if (ModelState.IsValid)  // THIS IS ALWAYS TRUE! even if i pass nothing<----
    {
            return Redirect("/");
    }

   return View(filingHistoryToCreate);
}

I read through a bunch of SO links and even went through MSDN, and I think I have everything setup correctly, i.e. namespaces are fine so there is no naked partial class stuff going on.

When my view renders I leave all the textboxes empty, I set a breakpoint to inspect by entity and nothing has been set, yet the model is valid. I also tested by entering some garbage into the textboxes to ensure the model binding was working fine, and it was...

I tried also testing using a console application, and I found out that you have to take an additional step of registering the MetaData type, but I beleive in MVC this isnt required. In case it helps anyone - the console app and registering meta data type didn't work for me either, so I'm thinking my buddy class may be the culprit?

Upvotes: 1

Views: 353

Answers (1)

nemesv
nemesv

Reputation: 139758

It seems the DataAnnotationsModelMetadataProvider is looking for the public properties when checks for the attributes.

Change your FilingHistoryId to public and it should work:

public class FilingHistoryMetaData
{           
   [Display(Name = "Filing History Id")]
   [Required(ErrorMessage = "Filing History Id is Required.")]
   public int FilingHistoryId { get; set; }    
   // Other properties snipped for Brevity
}

Upvotes: 2

Related Questions