[英]c# MVC: How to update user roles?
I have a viewmodel which exposes a few public virtual ICollection Roles { get; 我有一个viewmodel,它公开了一些公共的虚拟ICollection角色。 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: 为了进行测试,我尝试添加一个名为“ Roles”的隐藏字段,并输入“ Admin”和它的GUID,因为将其发送回服务器很重要:
<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! 但是,当传递到服务器时,我可以看到以这种方式创建了无效的模型状态-任何建议将不胜感激!
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
那些什么也买不到,只会在您验证模型时伤害您
In the controller, you are absolutely correct. 在控制器中,您绝对正确。 Get the role and call Roles.AddUserToRole().
获取角色并调用Roles.AddUserToRole()。
I think you're already on the right track :). 我认为您已经走对了:)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.