2017-07-01 127 views
0

大家好消息。我有問題,再次。我有個人信息塊,個人信息編輯塊和密碼塊。點擊按鈕後,我需要重新加載密碼塊。但我有一個問題,當我點擊按鈕時,我的頁面嘗試重新加載所有頁面。並且需要再次調用加載個人和密碼信息的方法。但是,我只需要在密碼塊中顯示消息並使用密碼進行操作,而不會影響個人信息塊。 (另外我需要在不影響密碼塊的情況下使用個人信息塊,但我認爲這將是下面的相同答案)。點擊表單中的按鈕後重新加載頁面的一部分asp.net

PersonalInf cotroller:

[HttpPost] 
    public ActionResult changePassword(PersonalPageModel personalPage) 
    { 
     var passwordDetauls = dbModel.USERS.Where(model => 
     model.vPassword == personalPage.usersModel.NewPassword 
      ).FirstOrDefault(); 

     if (passwordDetauls != null) 
     { 
      personalPage.usersModel.ChangePasswordErrorMessage = "Новый пароль совпадает со старым!"; 
      return View("~/Views/home/PersonalPage.cshtml", personalPage); 
     } 
     else 
     { 
      var userDetails = dbModel.USERS.Where(model => 
      model.vPassword == personalPage.usersModel.vPassword).FirstOrDefault(); 

      if (userDetails == null) 
      { 
       personalPage.usersModel.LoginErrorMessage = "Неверный пароль!"; 
       return View("~/Views/home/PersonalPage.cshtml", personalPage); 
      } 
      else 
      { 
       int iIdUsers = Convert.ToInt32(Session["iIdUsers"].ToString()); 

       //Change password here! 

       var query = from ord in dbModel.USERS 
          where ord.iIdUsers == iIdUsers 
          select ord; 

       foreach (var ord in query) 
       { 
        ord.vPassword = personalPage.usersModel.NewPassword; 
       } 

       try 
       { 
        dbModel.SaveChanges(); 
       } 
       catch(Exception exc) 
       { 
        personalPage.usersModel.ChangePasswordErrorMessage = exc.Message; 
       } 

       ViewBag.SuccessMessage = "Пароль был успешно изменен!"; 

       //Session["iIdUsers"] = userDetails.iIdUsers; 
       //Session["vNickName"] = userDetails.vNickName; 
       //return RedirectToAction("PersonalPage", "Home"); 

       return View("~/Views/home/PersonalPage.cshtml", personalPage); 
      } 
     } 
    } 

和視圖代碼,像autherize形式代碼:

@using (Html.BeginForm("changePersonalInf", "PersonalPage", FormMethod.Post)) 
       { 
        @Html.AntiForgeryToken() 

        <tr> 
         <td><p>Город:</p></td> 
         <td>@Html.EditorFor(model => model.personalInfModel.vCity, new { htmlAttributes = new { @class = "form-control" } })</td> 
        </tr> 
        <tr> 
         <td><p>ФИО:</p></td> 
         <td>@Html.EditorFor(model => model.personalInfModel.vFIO, new { htmlAttributes = new { @class = "form-control" } })</td> 
        </tr> 
        <tr> 
         <td><p>Индекс:</p></td> 
         <td>@Html.EditorFor(model => model.personalInfModel.vIndex, new { htmlAttributes = new { @class = "form-control" } })</td> 
        </tr> 
        <tr> 
         <td><p>Населенный пункт:</p></td> 
         <td>@Html.EditorFor(model => model.personalInfModel.vLocality, new { htmlAttributes = new { @class = "form-control" } })</td> 
        </tr> 
        <tr> 
         <td><p>Улица:</p></td> 
         <td>@Html.EditorFor(model => model.personalInfModel.vStreet, new { htmlAttributes = new { @class = "form-control" } })</td> 
        </tr> 
        <tr> 
         <td><p>Дом/корпус/строение:</p></td> 
         <td>@Html.EditorFor(model => model.personalInfModel.vHome, new { htmlAttributes = new { @class = "form-control" } })</td> 
        </tr> 
        <tr> 
         <td><p>Квартира:</p></td> 
         <td>@Html.EditorFor(model => model.personalInfModel.vApartment, new { htmlAttributes = new { @class = "form-control" } })</td> 
        </tr> 
        <tr> 
         <td><p>Телефон:</p></td> 
         <td>@Html.EditorFor(model => model.personalInfModel.vPhone, new { htmlAttributes = new { @class = "form-control" } })</td> 
        </tr> 


        <tr> 
         <td><input type="submit" value="Изменить" class="btn-problem" /></td> 
         <td><input type="reset" value="Очистка" class="btn-problem" /></td> 
        </tr> 
       } 

回答

0

把改變密碼塊中的局部視圖,改變的形式的Ajax形式:

@using (Html.BeginForm("changePersonalInf", "PersonalPage", FormMethod.Post)) { 

} 

@using (Ajax.BeginForm("changePersonalInf", "PersonalPage", null, new AjaxOptions { 
    UpdateTargetId = "id_of_your_change_password_block" 
}, new { role = "form", id = "AssignCoachForm" })) { 

} 

這會自動更新給定ID的塊。或者,你可以自己在JavaScript中做到這一點,並有更多的控制權:

@using (Ajax.BeginForm("changePersonalInf", "PersonalPage", null, new AjaxOptions { 
    OnSuccess = "your_js_success_function(data);", 
    OnFailure = "your_js_failure_function();", 
}, new { role = "form", id = "AssignCoachForm" })) { 

} 
+0

它不工作!我再次給出了空指針異常的問題。你能告訴我例如,當我改變它時,代碼將如何顯示? – Zicise

+0

網上有很多例子。試着實現其中一個,如果你有問題,發佈你的新代碼(也許在一個新的問題),所以我們可以幫助你。 –

相關問題