ASP.NET MVC:复杂patial视图的客户端验证

时间:2022-02-25 16:37:35

I have this viewmodel:

我有这个viewmodel:

public class Letter
{
  public strng Name{get;set;}

  [UIHint("UserSelector")]
  [Required]
  public List<int> Users{get;set;}
}

For rendering Letter viewmodel I use Html.EditorForModel(). EditorTemplate "UserSelector" is complex view and looks like this:
ASP.NET MVC:复杂patial视图的客户端验证
Before submitting the form I manually generate hidden field controls for selected users and it's obviously RequiredAttribute doesn't work for Users property. What's "true" way to force UserSelector view to "understand" RequiredAttribute?

为了渲染Letter viewmodel,我使用了Html.EditorForModel()。 EditorTemplate“UserSelector”是一个复杂的视图,如下所示:在提交表单之前,我为所选用户手动生成隐藏字段控件,显然RequiredAttribute不适用于Users属性。什么是强制UserSelector视图“理解”RequiredAttribute的“真实”方法?

1 个解决方案

#1


0  

you could have a another string with empty value at first, and then use it in a hidden input, then make it required. Every time client add a user from left to right, update that hidden input value like 'Usera,Userb ....'. So when client tries to submit the form, this hidden input must have value.

你可以先得到一个空值的另一个字符串,然后在隐藏的输入中使用它,然后使它成为必需的。每次客户端从左向右添加用户时,请更新隐藏的输入值,如“Usera,Userb ....”。因此,当客户端尝试提交表单时,此隐藏的输入必须具有值。

#1


0  

you could have a another string with empty value at first, and then use it in a hidden input, then make it required. Every time client add a user from left to right, update that hidden input value like 'Usera,Userb ....'. So when client tries to submit the form, this hidden input must have value.

你可以先得到一个空值的另一个字符串,然后在隐藏的输入中使用它,然后使它成为必需的。每次客户端从左向右添加用户时,请更新隐藏的输入值,如“Usera,Userb ....”。因此,当客户端尝试提交表单时,此隐藏的输入必须具有值。