2012-03-21 41 views
0

我开始使用nhibernate,我需要创建一个应用程序,它可以执行添加,更新和删除功能 我的添加和更新工作正常 但是,删除字段ineed将标志更改为True .. ..但一些如何,我无法做到这一点 我控制器在更新字段时出错

public ActionResult Delete(int id) 
    { 
     Product_Master pm = new Product_MasterService().GetProduct_Data(id); 
        return View(pm); 
    } 

[HttpDelete] 
    public ActionResult Delete(Product_Master Prod) 
    { 
     try 
     { 
      if (ModelState.IsValid) 
      { 
       new Product_MasterService().DeleteProd(Prod); 
       return RedirectToAction("Index"); 
      } 
      return View(Prod); 
     } 
     catch 
     { 
      return View(); 
     } 
    } 
在我看来

<h3>Are you sure you want to delete this?</h3> 

Hobby_Master

<div class="display-label">Product_Id</div> 
<div class="display-field"> 
    @Html.DisplayFor(model => model.product_Id) 
</div> 

<div class="display-label">Product_Name</div> 
<div class="display-field"> 
    @Html.DisplayFor(model => model.product_Name) 
</div> 
@using (Html.BeginForm()) { 
<p> 
    <input type="submit" value="Delete" /> | 
    @Html.ActionLink("Back to List", "Index") 
</p> 
} 

我已经使用NHibernate和MVC 和Product_MasterService() 编写的功能如下

public bool DeleteProd(Product_Master Prod) 
    { 
     log.Debug("Start"); 
     ISession session = DataAccessLayerHelper.OpenWriterSession(); 
     ITransaction transaction = session.BeginTransaction(); 

      try 
     { 
      Prod.Deleted = 'T'; 
      session.SaveOrUpdate(Prod); 
      transaction.Commit(); 

      return true; 
     } 
     catch (Exception ex) 
     { 
      if (transaction != null && transaction.IsActive) 
       transaction.Rollback(); 

      log.Error(ex); 
      return false; 
     } 
     finally 
     { 
      if (transaction != null) 
       transaction.Dispose(); 

      if (session != null && session.IsConnected) 
       session.Close(); 

      log.Debug("End"); 
     }   } 

任何一个可以请帮我与......

回答

0

你需要关闭你的会话,调用session.Close();

+0

我forgt复制的那部分......它在d码DER .... – 2012-03-21 05:33:42

+0

你把它回归后,所以它永远不会被调用。把它放在返回之前。 – ivowiblo 2012-03-21 05:34:25

+0

它在终于阻止....所以我想它会被称为反正...... – 2012-03-21 05:36:34

0

我认为ivowiblo是正确的,你不能从try/catch返回值。您可以使用局部变量:

 var hasSuccessSave = false; 
     try 
     { 
      Prod.Deleted = 'T'; 
      session.SaveOrUpdate(Prod); 
      transaction.Commit(); 

      hasSuccessSave = true; 
     } 
     catch (Exception ex) 
     { 
      if (transaction != null && transaction.IsActive) 
       transaction.Rollback(); 

      log.Error(ex); 
     } 
     finally 
     { 
      if (transaction != null) 
       transaction.Dispose(); 

      if (session != null && session.IsConnected) 
       session.Close(); 

      log.Debug("End"); 
     }  
     return hasSuccessSave;