Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 24th, 2009, 03: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,
  #2 (permalink)  
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)
  #3 (permalink)  
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
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 02:19 PM



All times are GMT -4. The time now is 07:59 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.