0

I want to validate my View Model in class-Level .

I am using a actionFilter. How do I use a data annotation? and how to inject the Access database?

A validation that would happen if the customer says it is already our customer or not.

I used action filter but I think it must have a way to use a DataAnnotation

Commented the code follows:

public class DadosAssinaturaFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var model = filterContext.ActionParameters.Values.FirstOrDefault(x => x.GetType() == typeof(DadosAssinatura)) as DadosAssinatura;
        var modelState = filterContext.Controller.ViewData.ModelState;
        if (model != null)
        {
            var jaSouCliente = modelState.FirstOrDefault(x => x.Key == "JaSouCliente");
            if (jaSouCliente.Key != null)  // select "Is Clilent" radiobutton ?
            if (jaSouCliente.Value.Errors.Count > 0) // if so remove the errors of the registration data
            {
                modelState.RemoveKeysStartsWith("DadosCliente.");
                modelState.RemoveKeysStartsWith("DadosAcesso.");
            }
            else if (model.JaSouCliente != null && model.JaSouCliente.Value) // else, click in "Is Client"
            {
                modelState.RemoveKeysStartsWith("DadosCliente."); //remove 

                modelState.Remove("DadosAcesso.ConfirmaSenha"); //how injec UnitOfWor/Repository? AutoFac?
               if (unitOfWork.Client.GetClientByUser(model.DadosAcesso.Usuario, model.DadosAcesso.Senha) == null)//user and Password
                modelState.AddModelError("DadosAcesso.Usuario", "Usuario Nao Encontrado");
            }
            else if (model.DadosCliente.PessoaFisica) // is a company our people?
            {
                modelState.Remove("DadosCliente.RazaoSocial"); // remove validate for company name
                modelState.Remove("DadosCliente.Cnpj"); //the brazilian document of company
            }
            else modelState.Remove("DadosCliente.Cpf"); //the brazilian document of people
        }

        base.OnActionExecuting(filterContext);
    }
}

public static class ModelStateErros
{

    public static void RemoveKeysStartsWith(this ModelStateDictionary modelStateDictionary, string startsWith)
    {
        var keys = modelStateDictionary.Keys.Where(key => key.StartsWith(startsWith)).ToList();
        foreach (var variable in keys)
        {
            modelStateDictionary.Remove(variable);
        }
    }
}

sorry my English

rekire
  • 47,260
  • 30
  • 167
  • 264
Fernando Mondo
  • 477
  • 3
  • 18

1 Answers1

0

Simply implement IValidateableObject in your ViewModel class (or create another partial class) and avoid the filter completely, and keep your validation logic with your ViewModel.

How do I use IValidatableObject?

Community
  • 1
  • 1
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • no, if its a problem you just dont raise the error rather than worry about removing them. this logic should exist in your view model (if its available there) since this is VIEW specific validation it seems. – Adam Tuliper Jan 06 '12 at 20:53