Wrox Programmer Forums
|
BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950
This is the forum to discuss the Wrox book ASP.NET MVC Website Programming Problem Design Solution by Nicholas Berardi, Al Katawazi, and Marco Bellinaso; ISBN: 9780470410950
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 24th, 2009, 02:35 PM
Registered User
 
Join Date: May 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default EditUser

I do not understand the current logic in the EditUser method of the UserController. When I update the user, all of the roles get checked regardless if it is selected or not. Here is the code (VB.Net):

Code:
<Authorize(Roles:="Admin")> _
<AcceptVerbs("POST")> _
 

PublicFunction EditUser(ByVal id AsString, ByVal approved AsBoolean) As ActionResult
'Is a list of all the user roles
Dim removeRoleList As ArrayList = New ArrayList(Roles.GetAllRoles())
'We are requesting the form variables directly from the form
ForEach key AsStringIn Request.Form.Keys
If key.StartsWith("role.") Then
Dim roleName AsString = key.Substring(5, key.Length - 5)
removeRoleList.Remove(roleName)
IfNot Roles.IsUserInRole(id, roleName) Then
Roles.AddUserToRole(id, roleName)
EndIf
EndIf
Next
ForEach removeRole AsStringIn removeRoleList
Roles.RemoveUserFromRole(id, removeRole)
Next
Dim membershipUser As MembershipUser = Membership.GetUser(id)
membershipUser.IsApproved = approved
Membership.UpdateUser(membershipUser)
TempData("SuccessMessage") = "User Information has been updated"
ViewData("roles") = DirectCast(Roles.GetAllRoles(), String())
ViewData("PageTitle") = "Edit " & id
Return View(membershipUser)
EndFunction
Thanks,
 
Old November 19th, 2009, 06:00 AM
Registered User
 
Join Date: Nov 2009
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
Default Controller dosn't read the value of your checkboxes

Quote:
Originally Posted by jpatrick View Post
I do not understand the current logic in the EditUser method of the UserController. When I update the user, all of the roles get checked regardless if it is selected or not. Here is the code (VB.Net):

Code:
<Authorize(Roles:="Admin")> _
<AcceptVerbs("POST")> _
 

PublicFunction EditUser(ByVal id AsString, ByVal approved AsBoolean) As ActionResult
'Is a list of all the user roles
Dim removeRoleList As ArrayList = New ArrayList(Roles.GetAllRoles())
'We are requesting the form variables directly from the form
ForEach key AsStringIn Request.Form.Keys
If key.StartsWith("role.") Then
Dim roleName AsString = key.Substring(5, key.Length - 5)
removeRoleList.Remove(roleName)
IfNot Roles.IsUserInRole(id, roleName) Then
Roles.AddUserToRole(id, roleName)
EndIf
EndIf
Next
ForEach removeRole AsStringIn removeRoleList
Roles.RemoveUserFromRole(id, removeRole)
Next
Dim membershipUser As MembershipUser = Membership.GetUser(id)
membershipUser.IsApproved = approved
Membership.UpdateUser(membershipUser)
TempData("SuccessMessage") = "User Information has been updated"
ViewData("roles") = DirectCast(Roles.GetAllRoles(), String())
ViewData("PageTitle") = "Edit " & id
Return View(membershipUser)
EndFunction
Thanks,
The code adds the user to all the roles in your site (?), however it dosn't remove the user form the roles you have unchecked in your form. Here is my C# code. And yes, I had to reverse the logic. My brain works the other way around.

Code:
            //Is a empty list of all the roles the user should have
            ArrayList addRoleList = new ArrayList();

            //We are requesting the form variables directly from the form
            foreach (string key in Request.Form.Keys)
            {
                if (key.StartsWith("role."))
                {
                    String userRole = key.Substring(5, key.Length - 5);
                    //System.Web.UI.WebControls.CheckBox test = Request.Form[key];
                    if (Roles.IsUserInRole(id, userRole))
                    {
                        Roles.RemoveUserFromRole(id, userRole);
                    }
                    //Checks the checkboxes 
                    if (Request.Form[key].Contains("true"))
                    {
                        addRoleList.Add(userRole);  
                    }
                }
            }
            foreach (string addRole in addRoleList)
                Roles.AddUserToRole(id, addRole)

Last edited by kaifr; November 19th, 2009 at 07:58 AM.. Reason: Typo
The Following User Says Thank You to kaifr For This Useful Post:
djlabowski (February 26th, 2011)
 
Old February 26th, 2011, 10:42 PM
Registered User
 
Join Date: Feb 2011
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Default Kaifr you're the man

Just saved me at least an hour. Lemme buy you a beer





Similar Threads
Thread Thread Starter Forum Replies Last Post
System.NullReferenceException in the EditUser View... philthy BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 6 July 27th, 2009 01:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.