Remote validation asp.net mvc

138 views Asked by At

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?

2

There are 2 answers

0
mreyeros On

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.

public JsonResult CheckForDuplicate<T>(string code) where T : IModelWithCode
{
    var data = db.Set<T>().Where(t => t.Code.Equals(code));
    ....
}

public interface IModelWithCode
{
    string Code { get; set; }
}

Hopefully that will get you started in the right direction.

1
Romias On

I don't think this can be done or at least easily.

I would stick with the simple here: create a Select Case and check in the tables depending on the parameter passed (model name).

Dim exist = false;

Select Case myModel
        Case "Model1"
            exist = db.Model1Table.Where(Function(p) p.code.Equals(code, StringComparison.CurrentCultureIgnoreCase)).Any()
        Case "Model2"
            exist = db.Model2Table.Where(Function(p) p.code.Equals(code, StringComparison.CurrentCultureIgnoreCase)).Any()
End Select

If each table has different layout or you have to do some other checks... you are free to do the special thing in each case.

UPDATE: Here you can see an article showing how to pass other fields to the validator action. You should create a Hidden Field to hold the Model name. http://www.codeproject.com/Articles/674288/Remote-Validation-in-MVC-Simple-Way-to-Pass-the-F

Other resource: MVC Remote Attribute Additional Fields