Generic validation for a MVC 5 template editor

0

I am working on an Editor template for an object, which must be validated independently of the rest of the form, this validation I am doing with ajax, I am a small example to show my problem.

class A
{
   public B b { get; set; }
}

class B
{
   public string CampoRandom { get; set; }
}

My problem is that the B be used as a generic type in A , as in any other class ( EditorFor(x => x.B) ) I must validate it in a generic way.

At the moment of sending it, I serialize only the part of the form that interests me to perform the validation in the following way:

$('#DIVB :input').serialize()

What actually serializes the fields that interest me, but the problem is that I can not get them to the controller.

My controller, it would be the next one

public JsonResult Validar(B b)
{
    if(TryUpdateModel(b)){
                      //Lógica
                         }
return Json(new { //más lógica })
}

My problem is that as you can see, my method expects B but in reality, it receives A.b , in some case, I could receive CualquierOtraClase.B and I have to be able to validate the data of B independently How can I handle this issue?

Clarification: The information of B arrives complete in case of receiving it as A , otherwise, null.

I know it's a cumbersome subject to explain, I hope that if you do not understand me correctly tell me and try to improve the question as much as possible!

Greetings and thank you very much!

Edit: I clarify that B would be obtained within a modal (which is part of the main form of A , that's why the whole problem is generated, and the validation would be executed at moment of '' Save Changes '' within the modal.

    
asked by Juan Salvador Portugal 28.08.2018 в 18:41
source

0 answers