动作控制器asp.net

时间:2012-09-20 16:57:23

标签: c# asp.net .net nhibernate razor

我做了这种代码艺术,我遇到了问题

  • 查询更新不起作用,我不知道为什么
  • 我总是被重定向到年龄指数 文件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;}

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

1 个答案:

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

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