2012-09-20 80 views
-1

我做這門藝術的代碼,我有一個問題動作控制器asp.net

  • 查詢更新不工作,我不知道爲什麼
  • 我總是重定向到年齡指標 文件Edit.cshtml
@model DATA2.User 
    @{ViewBag.Title = "Edit"; 
     Layout = "~/Views/Shared/_General.cshtml"; 
    } 
    @using (Html.BeginForm("Save", "Administration")) 
    { 
    <table> 
    <tr> 
     <td><label >        </label></td> 
       <td> 
        <label title= "Name " style="font-weight:bold" >    Name     </label> 
        </td> 
        <td><label title= "Email " style="font-weight:bold" >    Email     
</label></td> 
        <td><label title= "Password " style="font-weight:bold" >    Password     
</label></td> 
        <td><label title= "Phone" style="font-weight:bold" >    Phone     
</label></td> 
        </tr> 
        <tr > 
          <td><label style="font-weight:bold" >   Recent     </label></td> 
         <td><label >    @Model.ContactName     </label></td> 
        <td><label >    @Model.ContactEmail     </label></td> 
        <td><label >    @Model.Password     </label></td> 
        <td><label >    @Model.ContactPhone     </label></td> 
         </tr> 
       <tr> 
       <td><label style="font-weight:bold" >   New     </label></td> 
        <td><input id="nom" name= "Pseudo" style="width: 156px"/></td>  
        <td><input id="mail" name= "Pseudo" style="width: 156px"/></td>  
        <td><input id="mot" name= "Pseudo" style="width: 156px"/></td> 
        <td><input id="phone" name= "Pseudo" style="width: 156px"/></td> 
       </tr> 
        <tr> 
        <td><label id="id" style="visibility:hidden" >@Model.Identification</label></td> 
        <td><input type="submit" value="Modify"  style="width: 156px; position:relative" /></td> 
        </tr> 
     </table> 

} 

行動節省:

public ActionResult Save(string id, string nom, string mail, string phone, string mot) 
     { 

      if(c.UpdateUser(id, (c.GetPassword().Count + 1).ToString(), mot, "", nom, phone, mail)) return RedirectToAction("Admin", "Administration"); 
      else return RedirectToAction("Index", "Administration"); 

     } 

方法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;} 

總是我正在重定向到頁面索引,爲什麼?

+0

錯誤是「NHibernate.StaleObjectStateException:行被更新或被另一個事務刪除(或未保存值映射不正確):[DATA2.User#DATA2.User]」 – developper

+0

'!(String.IsNullOrEmpty(mot)''應該是'!String.IsNullOrEmpty(mot)' – Rippo

+0

所有的HTML似乎都與這個問題無關。 –

回答

0

通常你會首先從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; 

沒有必要呼籲會話關閉(),因爲這是由使用語句照顧。另外,在User類上調用OpenSession()看起來非常奇怪 - 它不屬於表示類的數據。