0

I have added a TeamID to the AspNetUsers table. There is a Teams table and every team is giving an ID. I want users to select a Team and when they are selected the TeamID in the AspNetUsers table to be updated using an action link to do this.

[HttpPost]
        public ActionResult Select(int? id)
        {
            Team team = db.Teams.Find(id);
            if (team != null)
            {

                ViewBag.RowsAffected = db.Database.ExecuteSqlCommand("UPDATE AspNetUsers SET TeamID = {0}", team);
            }
            return View();
        } 

This is the code I have tried but it does not work any suggestions or better ways to do it?

Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77
DaveReg
  • 23
  • 4

1 Answers1

0

That query is updating the Team column for EVERY user in your app.

You need to first get the current user (the one actually using the app), find her Id/name to use it in a WHERE clause.

UPDATE AspNetUsers SET TeamId = XXX where Id = YYYY

Too much going on for a simple answer. How to get the current User id is already answered here:

ASP.NET MVC 5 - Identity. How to get current ApplicationUser

Community
  • 1
  • 1
CharlieBrown
  • 4,143
  • 23
  • 24