编辑MVC4和Visual Studio 2012中的方法 [英] Edit Method in MVC4 and Visual Studio 2012
问题描述
除了发布内容之外,我还试过:
HttpPost Edit:
[HttpPost]
public ActionResult Edit(MensPlayer mensPlayer)
{
if(ModelState.IsValid)
{
//保存播放器
_dataSource.Entry(mensPlayer).State = EntityState.Modified;
_dataSource.Save();
TempData [message] = string.Format({0}已保存,mensPlayer.Name);
return RedirectToAction(Detail,MensPlayer,new {id = mensPlayer.Id});
}
return View(mensPlayer);
}
上述方法的问题在 Entry mensPlayer).State
,代码不会在.Entry()中构建,我似乎不知道这个方法是在继承的。请注意,这是MVC-Music-Store示例中使用的技术。
我还从Pro ASP.NET MVC3框架书中尝试过这种技术。
[HttpPost]
public ActionResult Edit(MensPlayer mensPlayer)
{
if(ModelState.IsValid)
{
//保存播放器
_dataSource.Save();
TempData [message] = string.Format({0}已保存,mensPlayer.Name);
return RedirectToAction(Detail,MensPlayer,new {id = mensPlayer.Id});
}
return View(mensPlayer);
}
此技术构建,但似乎没有保存对数据库的更改。 / p>
我该怎么办?我缺少什么?
如果有任何人需要查看整个解决方案,整个源代码都在GitHub上。
添加了 void Update();
到IDataSource并将以下内容添加到Db类
void IDataSource.Update()
{
条目(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}已保存,mensPlayer.Name);
return RedirectToAction(Detail,MensPlayer,new {id = mensPlayer.Id});
}
return View(mensPlayer);
}
所有版本,但是我收到这个错误
实体类型DbSet`1不是当前上下文模型的一部分。
据信来源于:
void IDataSource.Update()
{
条目(MensPlayers).State = EntityState.Modified;
}
在Slauma的帮助下上面的评论是他建议我做的,它的作品。
添加到IDataSource接口:
void Update(MensPlayer mensPlayer);
更新在Db类中实现:
void IDataSource.Update(MensPlayer mensPlayer)
{
条目(mensPlayer).State = EntityState.Modified;
}
编辑操作:
[HttpPost]
public ActionResult Edit(MensPlayer mensPlayer)
{
if(ModelState.IsValid)
{
/ / Save Player
_dataSource.Update(mensPlayer);
_dataSource.Save();
TempData [message] = string.Format({0}已保存,mensPlayer.Name);
return RedirectToAction(Detail,MensPlayer,new {id = mensPlayer.Id});
}
return View(mensPlayer);
}
只要这一切都正常,虽然我是在假设我可以实现对整个DbSet的更新,就像我用Save一样。
Could someone please help me with the question i posted late last night. I am still stuck with the edit method. Link below:
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.
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;
}
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.
这篇关于编辑MVC4和Visual Studio 2012中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!