【问题标题】:MVC ModelState Validation of List of Complex Properties复杂属性列表的 MVC ModelState 验证
【发布时间】:2016-01-06 11:48:40
【问题描述】:

所以,我有一个User 模型类。它有UnitViewModel的列表。

public class UserViewModel
{
    [Required(ErrorMessage="FirstName")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "LastName")]
    public string LastName { get; set; }

    public List<UnitViewModel> DesiredUnits { get; set; }
}

public class UnitViewModel
{
    public Guid? UnitID { get; set; }
    [Required(ErrorMessage = "NAMEEE")]
    public string Name { get; set; }

    public bool Selected { get; set; }
}

当我尝试创建新的User 时,我会从我的角度发布UserViewModel。在这里我得到ModelState.IsValidfalse。发生这种情况是因为验证还检查了为空的UnitViewModel.Name 属性(这是正常的,因为我创建了User 并且我有UnitViewModelcheckbox 列表。用户从现有的UnitViewModel 列表中选择UnitViewModel)。

我可以通过 foreach 循环以ModelState["DesiredUnits[0].UnitID"].Errors.Clear() 的可怕方式修复它,但我不会。

那么,解决方案是什么?如何禁用模型复杂属性列表的验证?

更新
我目前使用的另一个解决方案是在UnitViewModel 的编辑器模板中添加HiddenFor(m=&gt;m.Name),但这种方法的缺点是当我在编辑器模板中已经有标签时Name 是不必要的。此外,如果我有更多必填字段,我将必须为每个字段设置相应的隐藏输入。

【问题讨论】:

  • 您能否以没有任何字段链接 DesiredUnits 属性的方式呈现您的创建视图?这样 UnitViewModel 列表将保持为空,并且没有完成名称验证
  • 我有EditorTemplate 用于DesiredUnits,它有UnitIDName。在创建User 时,我必须展示它,因为他们需要选择所需的单位
  • 然后尝试使用可为空的 DesiredUnits
  • DesiredUnits 的类型是 List 所以,它本身可以为空

标签: c# asp.net-mvc validation modelstate complextype


【解决方案1】:

使用

   [HttpPost]       
    public ActionResult CreateEnquiryOnly([Bind(Include="FirstName,LastName")]UserViewModel userviewmodel)
    {
        if(ModelState.IsValid)
        {       
        }
    }

【讨论】:

    【解决方案2】:

    如果您的请求视图模型包含指定视图不需要的属性或验证规则,您应该为此创建一个新对象。

    // New model, specific for request
    public class UserRequestViewModel
    {
        [Required(ErrorMessage="FirstName")]
        public string FirstName { get; set; }
    
        [Required(ErrorMessage = "LastName")]
        public string LastName { get; set; }
    }
    
    public ActionResult Index(UserRequestViewModel requestModel)
    {
        //... do something
    
        // Get the values required to return to the view
        var responseModel = new UserViewModel();
        responseModel.DesiredUnits = new List<UnitViewModel>();
    
        // Return the response model
        return View(responseModel);
    }
    

    如果你这样做,任何为你的应用程序开发的人都清楚,什么是可以接受的。

    或者,如果您决定无法为此创建一个全新新模型,请将您的验证移至控制器操作中:

    public ActionResult Index(UserViewModel requestResponseModel)
    {
        // Perform validation
        for (var i = 0; i < requestResponseModel.DesiredUnits.Count(); i++)
        {
            var validationErrorKey = string.Format("DesiredUnits[{0}]", i);
    
            // Is the Name property empty?
            if (string.IsNullOrWhiteSpace(requestResponseModel.DesiredUnits[i].Name))
            {
    
    
                this.ModelState.AddModelError("Posted Name cannot be empty", validationErrorKey );
            }
        }
    
        if (this.ModelState.IsValid)
        {
            // There was a model validation error
        }
    
        //... do something
    
        // Return the response model
        return View(requestResponseModel);
    }
    

    作为最后的手段,您可以使用,但我不确定这是否会阻止验证字段 - 您需要尝试一下:

    public ActionResult Index([Bind(Exclude = "DesiredUnits")]UserViewModel requestResponseModel)
    {
        // ... do something
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-26
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 2012-01-24
      • 1970-01-01
      • 2012-08-31
      相关资源
      最近更新 更多