0

I have this codes which work when adding a new account by using an admin account:

[Authorize(Roles = "Owner")] [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(UserProfile userprofile) { if (ModelState.IsValid) { db.UserProfiles.Add(userprofile); db.SaveChanges(); return RedirectToAction("Index"); } return View(userprofile); } 

but when I add this line of code

Roles.AddUserToRole(userprofile.UserId.ToString(), "4"); 

I get an error of:

No user found was found that has the name "11".

What should I do? Thanks in advance!

2 Answers 2

1

You should use UserName not UserId, check MSDN on this method:

public static void AddUserToRole( string username, string roleName ) 

Notise username? Also could be that you should use roleName not RoleId.

Sign up to request clarification or add additional context in comments.

5 Comments

But it is connected to the webpages_UsersInRoles
and where do you adding this line then? i mean after SaveChanges method?
Yup. Actually, I have tried adding the line before and after the SaveChanges method but to no avail.
How to I convert the string parameters of the AddUserToRole method to int?
why do you need to convert them?
0

Found out the answer and it was just pretty simple. Firstly, you need to call your database and just add the following codes:

 webpages_UsersInRoles s = new webpages_UsersInRoles(); var userid = WebSecurity.GetUserId(model.UserName); s.RoleId = roles; s.UserId = userid; db2.webpages_UsersInRoles.Add(s); db2.SaveChanges(); 

These are the codes that I lack in the problem above. Thanks to those who helped!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.