问题
Could someone please help me with the question i posted late last night. I am still stuck with the edit method. Link below:
Last night question
In addition to what was posted, i have also tried:
HttpPost Edit:
[HttpPost]
public ActionResult Edit(MensPlayer mensPlayer)
{
if (ModelState.IsValid)
{
//Save Player
_dataSource.Entry(mensPlayer).State = EntityState.Modified;
_dataSource.Save();
TempData["message"] = string.Format("{0} has been saved", mensPlayer.Name);
return RedirectToAction("Detail", "MensPlayer", new {id = mensPlayer.Id});
}
return View(mensPlayer);
}
The Issue with the above method is around Entry(mensPlayer).State
, code will not build at .Entry() and i don't seem to know where this method is being inherited. Mind you, this is the technique used in MVC-Music-Store sample.
I have also tried this technique from Pro ASP.NET MVC3 Framework book.
[HttpPost]
public ActionResult Edit(MensPlayer mensPlayer)
{
if (ModelState.IsValid)
{
//Save Player
_dataSource.Save();
TempData["message"] = string.Format("{0} has been saved", mensPlayer.Name);
return RedirectToAction("Detail", "MensPlayer", new {id = mensPlayer.Id});
}
return View(mensPlayer);
}
This technique builds but doesn't seem to save changes to the database.
How can i make it work? What am i missing?
The entire source code is on GitHub if anyone needs to look at the entire solution.
GitHub
With Slauma's help below i have now modified as follows:
Added void Update();
to IDataSource and added the following to the Db class
void IDataSource.Update()
{
Entry(MensPlayers).State = EntityState.Modified;
}
HttpPost Edit:
[HttpPost]
public ActionResult Edit(MensPlayer mensPlayer)
{
if (ModelState.IsValid)
{
//Save Player
_dataSource.Update();
_dataSource.Save();
TempData["message"] = string.Format("{0} has been saved", mensPlayer.Name);
return RedirectToAction("Detail", "MensPlayer", new {id = mensPlayer.Id});
}
return View(mensPlayer);
}
All builds but then i get this error The entity type DbSet`1 is not part of the model for the current context.
Which is believed to originate from:
void IDataSource.Update()
{
Entry(MensPlayers).State = EntityState.Modified;
}
回答1:
With help of Slauma in the comments above this is what he suggested i do and it works.
Add to IDataSource Interface:
void Update(MensPlayer mensPlayer);
Update Implemented in Db class:
void IDataSource.Update(MensPlayer mensPlayer)
{
Entry(mensPlayer).State = EntityState.Modified;
}
Edit Action:
[HttpPost]
public ActionResult Edit(MensPlayer mensPlayer)
{
if (ModelState.IsValid)
{
//Save Player
_dataSource.Update(mensPlayer);
_dataSource.Save();
TempData["message"] = string.Format("{0} has been saved", mensPlayer.Name);
return RedirectToAction("Detail", "MensPlayer", new {id = mensPlayer.Id});
}
return View(mensPlayer);
}
And Just like that all works fine, although i was under the assumption that i could implement Update to the whole DbSet like i did with Save.
来源:https://stackoverflow.com/questions/12962737/edit-method-in-mvc4-and-visual-studio-2012