日期:2014-05-17  浏览次数:20497 次

MVC2.0编辑后保存问题
[code=C#][/code]
//
  // GET: /StoreManager/Edit/5
 
  public ActionResult Edit(int id)
  {
  var viewModel = new StoreManagerViewModel
  {
  Album = storeDB.Album.Single(a => a.AlbumId == id),
  Genres = storeDB.Genre.ToList(),
  Artists = storeDB.Artist.ToList()
  };
  return View(viewModel);
  }

  //
  // POST: /StoreManager/Edit/5

  [HttpPost]
  public ActionResult Edit(int id, FormCollection collection)
  {
  Album album = new Album ();
  try
  {
  UpdateModel(album, "Album");  
  // TODO: Add Edit logic here  
  int a = storeDB.SaveChanges();
  return RedirectToAction("Index");
  }
  catch
  {
  var viewModel = new StoreManagerViewModel
  {
  Album = album,
  Genres = storeDB.Genre.ToList(),
  Artists = storeDB.Artist.ToList()
  };
  return View(viewModel);
  }
  }
GET方法没问题,POST方法中storeDB.SaveChanges()执行成功,但是返回值为0.
也就是根本没有修改成功。求大神指点

------解决方案--------------------
首先根据ID得到对应的记录,然后修改该记录,最后提交修改
Album = storeDB.Album.Single(a => a.AlbumId == id),
//Alubm.字段1='';
//Alubm.字段2='';
storeDB.SaveChanges();
------解决方案--------------------
打段点看看主键ID是不是0
注意在前台用个HIDDEN存储主键
------解决方案--------------------
public void update(Wjy wjy)
{
Wjy updateObject = _db.Wjy.First(c => c.id == wjy.id);
updateObject.name = wjy.name;
updateObject.remark = wjy.remark;
_db.SaveChanges();
}

用我这种方法更新看看
------解决方案--------------------
[HttpPost]
public ActionResult Edit(int id, Album album)

  
如果按照
Album album = new Album ();
UpdateModel(album, "Album")

相当于新建一个Album,那UpdateModel更新给谁?

------解决方案--------------------
C# code

 Album album = new Album ();
  try
  {
  UpdateModel(album, "Album");   
}