0

I am trying to make my own ValidationAttribute but when I need the "value" it is null.

I have my ViewModel like this

public class PacienteVerificarVM : IIdentifiableObject
{
    [DniFormatoCorrecto]
    public string NumeroDocumento { get; set; }

}

and my controller method is

 public ActionResult ResultadoBusqueda(PacienteVerificarVM viewmodel)
 {
        if (!ModelState.IsValid)
            return RedirectToAction("CreateDocumento");

        return PartialView();
 }

and my Validation attribute is:

public class DniFormatoCorrecto : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, 
        ValidationContext validationContext)
    {
       return ValidationResult.Success;
    }
}

Any idea?

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
Claudio Gareca
  • 109
  • 1
  • 11
  • You can check if your request is well-formed, i.e., if it really contains NumeroDocument. Put a breakpoint in the controller action and check if the value is in the viewModel. Also, you can use chrome dev tools to check if the request is correct – Rui Jul 03 '14 at 15:44
  • hi, yes the "ValidationContext validationContext" has data like name to the property, but the value is null, the request is ok, i ve use chrome dev tools to analyze the request. – Claudio Gareca Jul 03 '14 at 16:27
  • So the value is in the POST and the ViewModel has a value for NumeroDocumento? – Mister Epic Jul 03 '14 at 16:30
  • 1
    @Shacka You say the request is ok, yet you get a null value in the validation attribute. Most likely the model binder is not binding the value in the request to the property in your viewModel. The easiest way to check this is to put a breakpoint in your **controller action** and check if *viewModel.NumeroDocumento* is not null, if it is, then your request is the problem. If this is the case check your view, specifically the name of the form field for NumeroDocumento. Most likely, you have a typo there. – Rui Jul 03 '14 at 17:11
  • 1
    @Shacka Can you post the code for your view, where the request originates? Does it show a list of patients? – Rui Jul 03 '14 at 17:13

0 Answers0