Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅当填充了任何文件时才验证两个模型_C#_Asp.net Mvc_Model - Fatal编程技术网

C# 仅当填充了任何文件时才验证两个模型

C# 仅当填充了任何文件时才验证两个模型,c#,asp.net-mvc,model,C#,Asp.net Mvc,Model,我有两个模型的视图:通用帐户e配置文件用户 public class AccountModel { [Display(Name = "UserId", Prompt = "UserId", ResourceType = typeof(Strings))] public string UserId { get; set; } [Required(ErrorMessageResourceName = "FieldRequired", ErrorMessageResource

我有两个模型的视图:通用帐户e配置文件用户

public class AccountModel
{
    [Display(Name = "UserId", Prompt = "UserId", ResourceType = typeof(Strings))]
    public string UserId { get; set; }

    [Required(ErrorMessageResourceName = "FieldRequired", ErrorMessageResourceType = typeof(Validation))]
    [EmailAddress(ErrorMessage = null, ErrorMessageResourceName = "InvalidEmail", ErrorMessageResourceType = typeof(Validation))]
    [Display(Name = "Email", Prompt = "Email", ResourceType = typeof(Strings))]
    public string Email { get; set; }

    [Required(ErrorMessageResourceName = "FieldRequired", ErrorMessageResourceType = typeof(Validation))]
    [StringLength(100, ErrorMessageResourceName = "FieldMaximumLength", ErrorMessageResourceType = typeof(Validation))]
    [Display(Name = "DisplayName", Prompt = "DisplayName", ResourceType = typeof(Strings))]
    public string DisplayName { get; set; }

    [Display(Name = "PhoneNumber", Prompt = "PhoneNumber", ResourceType = typeof(Strings))]
    public string PhoneNumber { get; set; }

    [Display(Name = "Country", Prompt = "Country", ResourceType = typeof(Strings))]
    public int CountryId { get; set; }

    public SelectList CountryList { get; set; }

}

public class UserProfileModel
{
    [Display(Name = "ID User", Prompt = "ID User")]
    public string IDUser { get; set; }

    [Required(ErrorMessageResourceName = "FieldRequired", ErrorMessageResourceType = typeof(Validation))]
    [StringLength(100, ErrorMessageResourceName = "FieldMaximumLength", ErrorMessageResourceType = typeof(Validation))]
    [Display(Name = "UserFullName", Prompt = "UserFullName")]
    public string UserFullName { get; set; }

    [Required(ErrorMessageResourceName = "FieldRequired", ErrorMessageResourceType = typeof(Validation))]
    [StringLength(16, ErrorMessageResourceName = "FieldMaximumLength", ErrorMessageResourceType = typeof(Validation))]
    [Display(Name = "Code", Prompt = "Code")]
    public string Code { get; set; }

    [Required(ErrorMessageResourceName = "FieldRequired", ErrorMessageResourceType = typeof(Validation))]
    [StringLength(250, ErrorMessageResourceName = "FieldMaximumLength", ErrorMessageResourceType = typeof(Validation))]
    [Display(Name = "Birth Place", Prompt = "BirthPlace")]
    public string BirthPlace { get; set; }

    [Display(Name = "Newsletter")]
    public bool? Newsletter { get; set; }

    [Display(Name = "Avatar", Prompt = "Avatar")]
    public string Avatar { get; set; }

}
我应该验证第二个模型,前提是至少有一个连接到他的字段已被填充。
例如,如果输入了出生地,则还要验证第二个模型,否则只验证第一个模型。

我希望我的要求是明确的

如果我理解您的问题,您可能想知道如何手动验证模型

您可以使用
TryValidateModel(modelInstance)调用以手动验证所需模型

对于问题中提到的其他条件,可以添加if语句

e、 g.如果userProfileModelObject的所有字符串属性都为空,则验证accountModelInstance

bool isSecondObjectValid = userProfileModelObject.GetType().GetProperties()
    .Where(pi => pi.PropertyType == typeof(string))
    .Select(pi => (string)pi.GetValue(myObject))
    .Any(value => string.IsNullOrEmpty(value));


bool validationResult = false;
validationResult = !isSecondObjectValid ? 
                          TryValidateModel(accountModelInstance) :
                          TryValidateModel(userProfileModelObject);
我希望这有帮助

参考文献:


    • 如果我理解您的问题,您可能想知道如何手动验证模型

      您可以使用
      TryValidateModel(modelInstance)调用以手动验证所需模型

      对于问题中提到的其他条件,可以添加if语句

      e、 g.如果userProfileModelObject的所有字符串属性都为空,则验证accountModelInstance

      bool isSecondObjectValid = userProfileModelObject.GetType().GetProperties()
          .Where(pi => pi.PropertyType == typeof(string))
          .Select(pi => (string)pi.GetValue(myObject))
          .Any(value => string.IsNullOrEmpty(value));
      
      
      bool validationResult = false;
      validationResult = !isSecondObjectValid ? 
                                TryValidateModel(accountModelInstance) :
                                TryValidateModel(userProfileModelObject);
      
      我希望这有帮助

      参考文献:


      谢谢您的帮助,但是如何将两个模型传递给控制器?您必须将它们封装在一个模型中,然后将其作为JSON传递到Post请求中。请注意,post请求不接受两个[FromBody]参数。为什么将请求作为JSON进行post?您只有两种方式-以查询字符串发送信息或post。最好将其发布,因为详细信息将显示在HTTP正文中,而不是URL中。JSON,因为您的API通常使用JSON格式与客户端交互。希望这有帮助。谢谢你的帮助,但是我如何将两个模型传递给控制器呢?你必须将它们封装在一个模型中,然后在Post请求中作为JSON传递。请注意,post请求不接受两个[FromBody]参数。为什么将请求作为JSON进行post?您只有两种方式-以查询字符串发送信息或post。最好将其发布,因为详细信息将显示在HTTP正文中,而不是URL中。JSON,因为您的API通常使用JSON格式与客户端交互。希望这有帮助。