目前我有一个用户可以部分创建的客户类,并完成详细信息。例如它看起来像这样
新客户
[Required(ErrorMessage = "Business name is required.")]
[Display(Name = "Business Name:")]
public string BusinessName { get; set; }
[Display(Name = "Address:")]
public string Address { get; set; }
[Display(Name = "City:")]
public string City { get; set; }
[Display(Name = "State:")]
public string State { get; set; }
[Display(Name = "Zip:")]
public string Zip { get; set; }
客户结帐
[Required(ErrorMessage = "Business name is required.")]
[Display(Name = "Business Name:")]
public string BusinessName { get; set; }
[Required(ErrorMessage = "Address is required.")]
[Display(Name = "Address:")]
public string Address { get; set; }
[Required(ErrorMessage = "City is required.")]
[Display(Name = "City:")]
public string City { get; set; }
[Required(ErrorMessage = "State is required.")]
[Display(Name = "State:")]
public string State { get; set; }
[Required(ErrorMessage = "Zip is required.")]
[Display(Name = "Zip:")]
public string Zip { get; set; }
当用户结帐时,我需要确保他们完成填写信息(如果缺少某些信息)。我的想法是创建一个 Customer Checkout 类,其中填充了他们创建新客户时的信息,并在将它们传递给结账之前检查它是否有效。我的问题是我知道的唯一方法是对照 string.isNullOrEmpty() 检查每个字段,但我知道必须有更好的方法。我将不得不检查 25 个这样的字段。我知道您可以检查模型是否有效,但我需要在我的数据访问层检查中的类级别执行此操作,以确保所有 [必需] 字段都有数据。希望我只是忽略了一些东西
几乎就像我需要一些方法来做类似的事情
bool hasErrors = false
foreach attribute in my class
if it is marked as [required]
check to make sure not null or empty
if it is set hasErrors = true
谢谢!