Im building a project where the user can add teams and select the team to add players.so when adding the players the ID of the team should be passed with it.I have a controller to add teams and another controller to add players.I passed the id of the team
to the Index action method of payer controller and displayed all players of that selected team.Now I want to add a player of that Team.I created a session in the Index action method to hold the ID and used it in the create Action method.when debugging I can
see that the Id is passed correct but when creating a new record, the player ID is always being 0 so an error on db.savechanges() occur as for playerID=0.
This is a snippet of my code:
Player Controller
public ActionResult Index(int id) { string team = db.Teams.Where(m => m.Id == id).Select(m => m.teamName).First(); var player = db.players.Where(m => m.team == team).ToList(); int Tid=db.Teams.Where(m=>m.Id==id).Select(m=>m.Id).FirstOrDefault(); Session["TeamID"] = Tid; return View(player); } [HttpPost] public ActionResult AddEditRecord(player Player, string cmd) { // team Id int TeamId = (int)Session["TeamID"]; //team name string team = db.Teams.Where(m => m.Id == TeamId).Select(m => m.teamName).First(); if (ModelState.IsValid) { if (cmd == "Save") { try { db.players.Add(Player); player pl = db.players.Where(m => m.team == null).FirstOrDefault(); pl.team = team; db.SaveChanges(); // db.SaveChanges(); return RedirectToAction("Index"); } catch { } } } } }the playerID is always set to zero.what is wrong with my code
I’m using partial views because I’m using a pop-up dialogue for CREUD operations
any help is really appreciated
Put a breakpoint in Index and step through the code to see if Tid is 0, you’ll probably find that it is which means the problem is with your Index query rather than where you get the data back from the Session
lolo512
string team = db.Teams.Where(m => m.Id == TeamId).Select(m => m.teamName).First();
[HttpPost] public ActionResult AddEditRecord(Player Plyr, string cmd) { // team Id int TeamId = (int)Session["TeamID"]; //team name Team team = db.Teams.FirstOrDefault(m => m.Id == TeamId); if(team==null) ModelState.AddModelError("","team not found"); if (ModelState.IsValid) { if (cmd == "Save") { try { Plyr.Team=team; db.players.Add(Plyr); db.SaveChanges(); return RedirectToAction("Index"); } catch { } } } } }
cnuonline
Plyr.Team=team;
Plyr.Team is expecting a string while team is not of type string, plus that my problem is not in saving the team, my problem is that when crreating the player(in the client side) the ID is not generated automatically so if I put a breakpoint on
public ActionResult AddEditRecord(player Player, string cmd)
I can see that Player has a zero value for its Id.
that is exactly my problem
AidyF
Put a breakpoint in Index and step through the code to see if Tid is 0, you’ll probably find that it is which means the problem is with your Index query rather than where you get the data back from the Session
I already traced my code and the Tid is correct giving me the ID and I even traced it in the AddEditRecordaction method
int TeamId = (int)Session["TeamID"];
and it also returns the selected ID my problem as I said earlier is in the player ID not the team ID
Now my question your player table I’m guessing the primary key on that table is of course the playerID, which auto increments when a new record is entered?
Am I on the right path?
Harrison.Scott
Am I on the right path?
yes that is true. this is my table
CREATE TABLE [dbo].[player] ( [Id] INT IDENTITY (1, 1) NOT NULL, [playerName] NVARCHAR (50) NOT NULL, [team] NVARCHAR (50) NOT NULL, [position] NVARCHAR (50) NOT NULL, [email] NVARCHAR (50) NOT NULL, [type] NVARCHAR (50) NOT NULL, [height] NVARCHAR (50) NOT NULL, PRIMARY KEY CLUSTERED ([Id] ASC) );
Excuse any syntax error I’m typing this on my phone
[HttpPost] public ActionResult AddEditRecord(player Player, string cmd) { // team Id int TeamId = (int)Session["TeamID"]; //team name string team = db.Teams.Where(m => m.Id == TeamId).Select(m => m.teamName).First(); if (ModelState.IsValid) { if (cmd == "Save") { try { var player = new Players() player.PlayerName = Player.PlayerName; player.Team == db.players.Where(m => m.team == null).FirstOrDefault();
player.position = Player.position;
player.email = Player.email;
player.type = Player.Type;
player.height = Player.height;
db.players.Add(player);
db.SaveChanges();
return RedirectToAction("Index");
}
catch { }
}
}
}
}
The above your not setting the ID as this is incremented in the DB so you only need to set the other values