0

I have this 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:
enter image description here
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?

mtkachenko
  • 5,389
  • 9
  • 38
  • 68

1 Answers1

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.

Babak Fakhriloo
  • 2,076
  • 4
  • 44
  • 80
  • Does it mean I must handle validation in complex views by myself: to add required attributes for hidden fields, show validation message etc.? – mtkachenko Mar 30 '14 at 06:57
  • No. For your case, just put a string and add a Required attribute for that, MVC does the validation for you. – Babak Fakhriloo Mar 30 '14 at 06:59
  • before submit I generate per hidden input for each id: if I select tom(id:4) and mark(id:5) then i create and . Do you want to use one hidden field for all values? Is it required additional parsing to cast it to List? – mtkachenko Mar 31 '14 at 14:15
  • Yes you need only on hidden field, which you can populate each time client choose one user (using jquery or js) then post this with your form to your action. There you can easily parse it to list of int variables – Babak Fakhriloo Apr 01 '14 at 06:10
  • I like my way because it's not required additional parsing and model binding works out of the box. Here is http://stackoverflow.com/questions/6428907/required-attribute-on-generic-list-property/6429119#6429119 post about custom attribute for such purpose (required for list). – mtkachenko Apr 01 '14 at 07:48