如何确定我的模型是否通过所有数据注释规则检查

本文关键字:数据 注释 规则 检查 我的 何确定 模型 是否 | 更新日期: 2023-09-27 18:31:40

我想知道我的模型是否有效。

这是我的模型

public class CallPartyModel
{
    public System.Guid PartyId { get; set; }
    public System.Guid FwCallMasterId { get; set; }
    [Required(ErrorMessage = "Principal Party is required.")]
    [Display(Name = "Principal Party")]
    public System.Guid PrincipalPartyId { get; set; }
    [Required(ErrorMessage = "Responsible Party is required.")]
    [Display(Name = "Responsible Party")]
    public System.Guid ResponsiblePartyId { get; set; }
    [Display(Name = "File Type")]
    public System.Guid FileTypeId { get; set; }
    [Display(Name = "Agent Type")]
    public Nullable<System.Guid> AgentTypeId { get; set; }
    public string AgentTypeCode { get; set; }
    public bool AdvancedRequired { get; set; }
    public bool SeperateDARequired { get; set; }
    public string PrincipalPartyName { get; set; }
    public string ResponsiblePartyName { get; set; }
    public string PrincipalReferenceCode { get; set; }
    public string ResponsibleReferenceCode { get; set; }
    public string FileTypeName { get; set; }
    public string FileTypeCode { get; set; }
    public string AgentTypeName { get; set; }
    public bool? DAIssuedFlag { get; set; }
    [Range(0, 999999999.999, ErrorMessage = "Value lies outside the 0 to 999999999.999 range")]
    public decimal? AdvanceReceivedAmount { get; set; }
    public System.Guid CreatedBy { get; set; }
    public System.DateTime CreatedDateTime { get; set; }
    public Nullable<System.Guid> ModifiedBy { get; set; }
    public Nullable<System.DateTime> ModifiedDateTime { get; set; }
    public bool IsDeleted { get; set; }
    public Nullable<System.Guid> DeletedBy { get; set; }
    public Nullable<System.DateTime> DeletedDateTime { get; set; }
    //public virtual UserModel FwCore_Users { get; set; } //Created By User
    //public virtual UserModel FwCore_Users1 { get; set; }//Modified By User
    //public virtual UserModel FwCore_Users2 { get; set; }// Deleted by User
    public bool IsDirtyCheck { get; set; }
    public bool LockPrinFlag { get; set; }
    public string LockPrinMsg { get; set; }
}

我已经为这个前任定义了一些规则。 public decimal? AdvanceReceivedAmount { get; set; }范围规则。我知道如何在我们的模型绑定时检查模型状态以显示为ModelState.Isvalid()但是在我的代码中,我正在使用拖曳困难模型,它在某些 wcf 服务中,我以所有属性的字符串形式获取输入,并且我无法在第二个模型上定义数据注释规则。因此,我必须手动将数据从模型一传输到模型二,并且在模型二(CallPartyModel)中,我已经定义了数据注释规则。现在,在数据库中执行任何事务之前,我必须检查模型属性的值是否有效,我知道我可以手动执行此操作,但是对于这种情况,是否有任何方法可以modelState.IsValid()

as: 
CallPartyModel obj=new CallPartyModel();
obj.AdvanceReceivedAmount=88.88;
if(obj.IsValid())
{
//go
}
else
{
//Show the error according to property
}

任何建议或帮助将不胜感激

如何确定我的模型是否通过所有数据注释规则检查

如何通过

加载模型 2 中的模型 1 来检查模型 1 与模型 2 的关系

,然后使用
 Model2 m2 = new Model2();
 //... load up the values into m2 from Model1
 if(TryUpdateModel(m2))  //if it is ok (checks validation)
 {
    ... your code...
 }

我希望这有所帮助。