I am having a problem being able to edit a column in a table, even though I can add and delete them quite easily. I ahve done this sort of thing before and had no problems with it, so I am stumped to as why it is happening now.
I can click on a players name and get his details up, but when I edit the players details in the view and click save, its saying the Sequence contains no elements.
Can someone please help me rectify this problem as it is driving me crazy, it would be much appreciated as it is most likely a simple solution.
Here is my code:
Repository:
public Players PlayersDetails(String Name)
{
var players = (from p in _entities.Players
where p.FullName == Name
select p).First();
return players;
}
public void playersEdit(Players playersToEdit)
{
var originalPlayer = PlayersDetails(playersToEdit.FullName);
_entities.ApplyPropertyChanges(originalPlayer.EntityKey.EntitySetName, playersToEdit);
_entities.SaveChanges();
}
IRepository:
public interface IHuddRLRepository
{
//Player List
IEnumerable<Players> PlayersList();
Players PlayersDetails(String Name);
void playersCreate(Players playersToCreate);
void playersEdit(Players playersToEdit);
void playersDelete(Players playersToDelete);
}
Controller:
public ActionResult Edit(String FullName)
{
Players players = _entities.PlayerDetails(FullName);
return View(players);
}
[ValidateInput(false)]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Players playersToEdit)
{
if (!ModelState.IsValid)
{
return View();
}
else
{
_entities.playersEdit(playersToEdit);
return RedirectToAction("Index", "Players");
}
}
Sequence contains no elements means that the query didn’t return a result when you were expecting one (using First() instead of FirstOrDefault() which returns null when no elements are found). I’m guessing that since you try to do a lookup using the edited player data, you must have changed the player’s name, which wouldn’t exist in the database yet.
What you should be doing is using an ID for the player lookup since that wouldn’t be editable and would refer to the data you are trying to edit.