更新字段时出错

时间:2012-03-21 05:21:55

标签: asp.net-mvc-3 nhibernate

我从nhibernate开始,我需要创建一个执行添加,更新和删除功能的应用程序 我的添加和更新工作正常 但是删除字段时,无法将标志更改为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");
         }            }

任何人都可以帮助我......

2 个答案:

答案 0 :(得分:0)

您需要关闭会话调用session.Close();

答案 1 :(得分: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;