0

I have a viewmodel which exposes a few public virtual ICollection Roles { get; set; }

This is great for bringing back what roles a user belongs to.. But how do I update the roles of a user from a form.

For testing I've tried adding a hidden field called "Roles" and entered both "Admin" and it's GUID as it's value to send back to the server:

<input type="hidden" name="Roles" value="Admin" />
<input type="hidden" name="Roles" value="7feab829-b86c-4a94-90bf-f35e9ce04b01" />

Howwver, when passes to the server I can see that this way creates an invalid modelstate -- Any suggestions would be greatly appreciated!

tereško
  • 58,060
  • 25
  • 98
  • 150
Simon Hopwood
  • 83
  • 2
  • 9
  • 1
    Why would you update their roles from the form? If you want to update roles, you should do it through an entire separate (and authenticated) controller action, not a return from the client. Remember, never trust the client. – David L Jun 10 '13 at 15:16
  • I'm creating the controller to update user (name/ password/ etc).. I thought this'd be a great place to also update the roles. It'll all be authenticated. – Simon Hopwood Jun 10 '13 at 16:01
  • I can understand allowing the user to update their name and password but do you also want them to be able to update their roles? I just want to make sure I understand your intent. In addition, are you using the default membership provider? – David L Jun 10 '13 at 16:05
  • This is not for the end user - This is a separate admin section to update users of the system :) I see why you were worried without understanding this :) – Simon Hopwood Jun 10 '13 at 16:08
  • Gotcha. In that case, can you post your controller? I suspect that you'll need a drop down list of available roles, as well as something on your post model that has the drop down list role selection. – David L Jun 10 '13 at 16:11
  • My controller is very lightweight -- http://pastie.org/8030944 WebSecurity.Register registers a created user, then I plan on calling Roles.AddUserToRole(, ); to add the required role.. Should I simply pass these over as strings? And write some code to parse them in the controller? – Simon Hopwood Jun 10 '13 at 16:18

1 Answers1

0

In light of your comments, I see a few straightforward solutions. First, remove the hidden input fields. Those don't buy you anything and only hurt you when you go to validate the model

  1. Utilize drop down lists for roles and pass it back up on your model. The value should be the selection.
  2. Pass them up as strings and parse in the controller, as you mentioned
  3. Use radial options, each with a unique ID. Pass up the ID and correspond that ID to a value in your controller.

In the controller, you are absolutely correct. Get the role and call Roles.AddUserToRole().

I think you're already on the right track :).

David L
  • 32,885
  • 8
  • 62
  • 93
  • My issue is that I'm using a viewmodel to return the user which includes: public virtual ICollection Roles { get; set; } Passing a string of roles back will not be valid for this model, will it? – Simon Hopwood Jun 10 '13 at 16:55
  • Hmm, so why send them down if you're just going to send them back up? You should consider then doing a check box system to send up List, or List, or some other list, or just allowing one role add/remove per call, OR move the roles out entirely – David L Jun 10 '13 at 17:03