ViewModel 类级验证

发布于 2024-12-25 01:34:39 字数 2468 浏览 0 评论 0原文

我想在类级别验证我的视图模型。

我正在使用动作过滤器。如何使用数据注释? 以及如何注入Access数据库?

如果客户说他是否已经是我们的客户,就会发生验证。

我使用了动作过滤器,但我认为它必须有一种使用 DataAnnotation 的方法

评论代码如下:

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);
        }
    }
}

对不起我的英语

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剧终人散尽 2025-01-01 01:34:39

只需在 ViewModel 类中实现 IValidateableObject(或创建另一个分部类)并完全避免过滤器,并将验证逻辑保留在 ViewModel 中。

如何使用 IValidatableObject?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文