C# 动作控制器asp.net

C# 动作控制器asp.net,c#,asp.net,.net,nhibernate,razor,C#,Asp.net,.net,Nhibernate,Razor,我做了这种艺术的代码,我有一个问题 查询更新不起作用,我不知道为什么 我总是被重定向到年龄索引 文件Edit.cshtml UpdateUser方法 public bool UpdateUser(string identification, string acc, string mot, string notify, string nom, string phone, string mail) { if ((!String.IsNullOrEmpty(ide

我做了这种艺术的代码,我有一个问题

  • 查询更新不起作用,我不知道为什么
  • 我总是被重定向到年龄索引 文件Edit.cshtml
UpdateUser方法

public bool UpdateUser(string identification, string acc, string mot, string notify, string nom, string phone, string mail)
       {
          if ((!String.IsNullOrEmpty(identification)) && (!String.IsNullOrEmpty(acc)) && (!String.IsNullOrEmpty(nom)) && (!String.IsNullOrEmpty(mail)) && !(String.IsNullOrEmpty(mot)) && Email_Exist(mail) == -1)
               {
                   using (NHibernate.ISession nhSession = User.OpenSession())
                   {
                       nhSession.Transaction.Begin();
                       User u = new User() { Account = acc, Identification = identification, ContactEmail = mail, ContactName = nom, ContactPhone = phone, NotifyEmail = notify, Password = mot };
                       nhSession.Update(u);
                       nhSession.Transaction.Commit();
                       nhSession.Close();
                       return true;
                   }
               }
               return false;}

我总是被重定向到页面索引为什么?

通常您会首先从NHibernate加载对象,然后更改该实例中的属性。你也在给NHibernate打一些无用的电话:

using (NHibernate.ISession nhSession = User.OpenSession())
using (var trans = nhSession.BeginTransaction())
{
    User u = nhSession.Get<User>(id);

    // Now update non-identifier fields as you wish.

    // Since the instance is already known by NH, the following
    // is enough (with default NH settings) to persist the changes.
    trans.Commit();
}

return true;
使用(NHibernate.ISession nhSession=User.OpenSession())
使用(var trans=nhSession.BeginTransaction())
{
用户u=nhSession.Get(id);
//现在根据需要更新非标识符字段。
//由于NH已经知道该实例,因此
//足够(使用默认NH设置)保留更改。
trans.Commit();
}
返回true;

不需要对会话调用Close(),因为这由using语句负责。另外,在用户类上调用OpenSession()看起来非常奇怪,因为它不属于表示类的数据。

错误是“NHibernate.StaleObjectStateException:行被另一个事务更新或删除(或者未保存的值映射不正确):[DATA2.User#DATA2.User]”
!(String.IsNullOrEmpty(mot)
应该是
!String.IsNullOrEmpty(mot)
所有的HTML似乎都与这个问题无关。
public bool UpdateUser(string identification, string acc, string mot, string notify, string nom, string phone, string mail)
       {
          if ((!String.IsNullOrEmpty(identification)) && (!String.IsNullOrEmpty(acc)) && (!String.IsNullOrEmpty(nom)) && (!String.IsNullOrEmpty(mail)) && !(String.IsNullOrEmpty(mot)) && Email_Exist(mail) == -1)
               {
                   using (NHibernate.ISession nhSession = User.OpenSession())
                   {
                       nhSession.Transaction.Begin();
                       User u = new User() { Account = acc, Identification = identification, ContactEmail = mail, ContactName = nom, ContactPhone = phone, NotifyEmail = notify, Password = mot };
                       nhSession.Update(u);
                       nhSession.Transaction.Commit();
                       nhSession.Close();
                       return true;
                   }
               }
               return false;}
using (NHibernate.ISession nhSession = User.OpenSession())
using (var trans = nhSession.BeginTransaction())
{
    User u = nhSession.Get<User>(id);

    // Now update non-identifier fields as you wish.

    // Since the instance is already known by NH, the following
    // is enough (with default NH settings) to persist the changes.
    trans.Commit();
}

return true;