2014-12-31 82 views
0

我在MVC應用程序的工作,我有一個ActionLinkedit果然具體行插入文本框和saveActionLink出現,而不是Edit該行上,但是當我做出改變和save點擊數據不會保存在數據庫中,我只會看到舊的數據。更新數據 - MVC

jQuery代碼:

<script type="text/javascript"> 
    $(document).ready(function() { 
     function toggleEditability() { 
      $(this).closest('tr') 
        .find('a.Edit, a.Save, .displayText, input[type=text]') 
        .toggle(); 
     } 

     $('a.Edit').click(toggleEditability); 

     $('a.Save').click(function() { 
      toggleEditability.call(this); 
      var url = $(this).attr('href'); 
      $(this).load(url); 
     }); 
    }); 
</script> 

CHTML代碼:

<table> 
     <tr> 
      @*<th> 
       @Html.Label("ID") 
      </th>*@ 
      <th> 
       @Html.Label("Name") 
      </th> 
      <th> 
       @Html.Label("Description") 
      </th> 
      <th> 
       @Html.Label("Date") 
      </th> 
      <th></th> 
     </tr> 

    @foreach (var item in Model) 
    { 
     if (Convert.ToDateTime(item.Holiday_date).Year.ToString() == DateTime.Now.Year.ToString()) 
     { 
      <tr> 
      @* <td> 
       @Html.TextBoxFor(modelItem => item.Holiday_Id, new { style = "display: none; width:170px; height:15px" }) 
       <div class="displaytext"> 
        @Html.DisplayFor(modelItem => item.Holiday_Id) 
       </div> 
      </td>*@ 
      <td> 
       @Html.TextBoxFor(modelItem => item.Holiday_Name, new { style = "display: none; width:170px; height:15px" }) 
       <div class="displaytext"> 
        @Html.DisplayFor(modelItem => item.Holiday_Name) 
       </div> 
      </td> 
      <td> 
       @Html.TextBoxFor(modelItem => item.Holiday_Description, new { style = "display: none; width:170px; height:15px" }) 
       <div class="displaytext"> 
        @Html.DisplayFor(modelItem => item.Holiday_Description) 
       </div> 
      </td> 
      <td> 
       @Html.TextBoxFor(modelItem => item.Holiday_date, new { style = "display: none; width:170px; height:15px" }) 
       <div class="displaytext"> 
        @Html.DisplayFor(modelItem => item.Holiday_date) 
       </div> 
      </td> 
      <td> 
       @Html.ActionLink("Edit", "Edit", new { id = item.Holiday_Id }, new { @class = "Edit", Href="#" }) 
       @Html.ActionLink("Save", "Save", new { id = item.Holiday_Id}, new { @class = "Save", Href = "#", style = "display:none" }) | 
       @Html.ActionLink("Delete", "Delete", new { id = item.Holiday_Id }, new { @class = "lnkDelete" }) 
       @Html.ActionLink("Cancel", "Cancel", new { id = item.Holiday_Id}, new { @class = "Cancel", Href = "#", style = "display:none" }) 
      </td> 
     </tr> 
     } 

    } 

    </table> 

控制器代碼編輯:

[HttpGet] 
     public ActionResult Edit(int id = 0) 
     { 
      tbl_HolidayList tbl_holidaylist = db.tbl_HolidayList.Find(id); 
      if (tbl_holidaylist == null) 
      { 
       return HttpNotFound(); 
      } 
      return PartialView(tbl_holidaylist); 
     } 

     // 
     // POST: /Holiday/Edit/5 

     [HttpPost] 
     public ActionResult Edit(tbl_HolidayList tbl_holidaylist) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Entry(tbl_holidaylist).State = EntityState.Modified; 
       db.SaveChanges(); 
       TempData["Msg"] = "Data has been updated succeessfully"; 
       return RedirectToAction("Index"); 
      } 
      return PartialView(tbl_holidaylist); 
     } 

誰能告訴我在哪裏,我誤認或者我必須做出變化??

+0

您正在使用'.load'向服務器發送'GET'請求。這不會發送任何更改。您需要使用'$ .ajax'' POST'請求​​發送更改(以及與之一起發送的相應'data'屬性)。 –

+0

您能否提一下我在哪裏以及如何進行更改? –

+0

下面添加的例子可以幫助您找到正確的方向,但可能並不符合您的具體要求。 –

回答

2

來自評論:您正在使用.load向服務器發送GET請求。這不會發送任何更改。您需要使用$.ajaxPOST請求(以及與之一起發送的相應data屬性)來發送更改。

例如像這樣的東西(不準確,但給你的想法):

$('a.Save').click(function() { 
     toggleEditability.call(this); 
     // Remember the element clicked 
     var $element $(this); 
     // Get all the data from the nearest form 
     var data = $(this).closest('form').serialize(); 
     // Get the URL from the form 
     var url = $(this).attr('href'); 
     $.ajax({ 
      url: url,    // Url to send request to 
      data: data,   // Send the data to the server 
      type: "POST",   // Make it a POST request 
      dataType: "html",  // Expect back HTML 
      success: function(html){ 
       $(element).html(html);  // On success put the HTML "somewhere" (not sure where at this point) 
      }); 
     }); 
    }); 

另一個問題是你的看法。您無法使用foreach循環渲染重複項目的表單。這並不能爲EditorFor類型方法提供足夠的信息來呈現索引(允許它唯一地命名項目)。您需要使用簡單的for循環:

例如,

for (int i = 0; i < Mode.Length; i++){ 
    { 
     @Html.TextBoxFor(modelItem => Model[i].Holiday_Id, new { style = "display: none; width:170px; height:15px" }) 
     [snip] 
    } 
+0

我根據我的要求做了這些改變,但'null'正在我的控制器方法中進行。 –

+0

您的示例頁面不完整。你能確保顯示整個視圖和'tbl_HolidayList'類嗎? –

+0

這是我的'tbl_HolidayList'類,它的工作正常,但數據在編輯方法中爲空 'public partial class tbl_HolidayList { public int Holiday_Id {get;組; } public string Holiday_Name {get;組; } public string Holiday_Description {get;組; } public Nullable Holiday_date {get;組; } }' –