General checks in MVC 5

I am developing an MVC 5 application using the first approach to the EF 6 database. I have a specific check that is required for fields in many of my models. I use remote verification to verify them. Since it was used in many models, so I'm trying to use a generic method.

In this regard, I created the IEntity interface, which includes all the properties used in my models. Then, for my validation method, I did the following:

[HttpPost] public JsonResult UniqueCheck<T>(T code) where T : class, IEntity { MyEntities db = new MyEntities(); if (db.Set<T>().Any(x=>x.S1 == code.S1)) { return Json("Already Exists!"); } else { return Json(true); } } 

And here is what I call property checking in models:

 [Remote("UniqueCheck", "Rules", HttpMethod = "POST")] public string S1 { get; set; } 

But the problem is that the check does not work, and when I checked in the browser console, I realized that the check really goes to the method, but 500 (Internal Server Error) was returned.

I know that the problem is with T written with the method name, because when I deleted generics and hardcoded my model name, it works fine.

I want to use only remote MVC confirmation, and I would be very happy to receive this general method, because otherwise it will be copy / paste in many places.

+1
source share

All Articles