如何为包含列表< T>的模型创建自定义验证 [英] How to create custom validation for a model that includes list<T>

查看:73
本文介绍了如何为包含列表< T>的模型创建自定义验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Parameter
{
  public int Id {get;set;}
  public string name {get;set;}
  public List<Phonenumbers> number {get;set;}
}

public class Phonenumbers
{
  public string number1 {get;set;}
  public string number2 {get;set;}
}


public Task<bool> Content(Parameter par)
{
  if(string.IsNullOrWhiteSpace(par.Id).ToString()))
    {
      //Todo
    }
 if(string.IsNullOrWhiteSpace(par.name))
    {
      //Todo 
    }
   
//I want to also validate List contents (number1,number2) fields if IsNullOrWhiteSpace
}





我尝试过:





What I have tried:

<pre>

public class Parameter
{
  public int Id {get;set;}
  public string name {get;set;}
  public List<Phonenumbers> number {get;set}
}

public class Phonenumbers
{
  public string number1 {get;set;}
  public string number2 {get;set;}
}


public Task<bool> Content(Parameter par)
{
  if(string.IsNullOrWhiteSpace(par.Id).ToString()))
    {
      //Todo
    }
 if(string.IsNullOrWhiteSpace(par.name))
    {
      //Todo 
    }
 if(string.IsNullOrWhiteSpace(par.Phonenumbers.Any(x => x.number1== null).ToString()))
    {
      //Todo 
    }
}

推荐答案

我会稍微改变你的代码。我已将此处的返回类型更改为 bool ,因为我不确定任务< bool> 正在做什么这里



I would change your code here slightly. I have changed the return type here to just a bool as I am not sure what the Task<bool> is doing here

public bool Content(Parameter par)
{
  if(par != null)
  {
    //int can not be null but it can have a default number
    //so test for that, unless the type is int? then you can 
    //test for null directly
    if(par.Id != 0)
    {
       //Todo
    }

    if(String.IsNullOrWhileSpave(par.name))
    {
      //Todo
    }

    if(par.Phonenumbers != null)
    {
      var failed = par.Phonenumbers.Count(p=> String.IsNullOrEmpty(p.number1) || String.IsNullOrEmpty(p.number2));
      if(failed > 0) 
      {
        //todo
      }
    }
  }
}


将Id字段从int转换为字符串没有意义 - 它将默认为0如果你没有设置它。因此,您应该检查它是否在有效范围内(例如大于0)。



您需要检查 par object不为null。否则,当对象为空时,您将获得Null Reference Exception。

如果 number 的计数为0,那么它将找不到任何电话数字。





There is no point in converting the Id field from int to string - it will default to 0 if you don't set it. Therefore you should just check that it is within a valid range (e.g. greater than 0).

You need to check that the par object is not null before attempted to access the numbers property. Otherwise you will get a Null Reference Exception when the object is null.
Also if the count of numbers is 0 then it will not find any phone numbers.


// Int will always be populated (it defaults to 0 if you don't set it).
if (par.Id <= 0)
{
    Console.WriteLine("1");
}
if (string.IsNullOrWhiteSpace(par.name))
{
    Console.WriteLine("2");
}
if (par.number == null ||
    par.number.Count == 0 ||
    par.number.Any(x => x.number1 != null || x.number2 != null)
    )
{
    Console.WriteLine("3");
}


这篇关于如何为包含列表&lt; T&gt;的模型创建自定义验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆