I am new in mvc so forgive me if the question is stupid but I want to do the best I can. So, my situation is that: I have created a model and decorated like
Partial Public Class App_Modules
<Required>
<Remote("CheckForDuplicate", "Validation")>
<Display(Name:="Code")>
Public Property code As String
<Required>
<Display(Name:="Description")>
Public Property name As String
End Class
As you can see, the code column must be remote validated. In my ValidationController I have the code
Public Function CheckForDuplicate(code As String) As JsonResult
Dim data = db.App_Modules.Where(Function(p) p.code.Equals(code, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault()
If data IsNot Nothing Then
Return Json("This code already exists",JsonRequestBehavior.AllowGet)
Else
Return Json(True, JsonRequestBehavior.AllowGet)
End If
End Function
End Class
Everything works fine! Now I want to do the same for another model with the same field "code". Is there any way to pass the model name to the function so instead of the line
Dim data = db.**App_Modules**.Where(Function(p) p.code.Equals(code, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault()
I could have something like
Dim data = db.**TABLENAME**.Where(Function(p) p.code.Equals(code, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault()
So the function would be generic and can be called from other models too?
I am not sure of the syntax in VB but you could modify your CheckForDuplicate function to accept a generic parameter that represents your Model class and pass that to the Set function of your DBContext. you will also need to define an interface for your Model that contains the Code property. Sample code in c# is as follows.
Hopefully that will get you started in the right direction.