2014-12-31 56 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;组; } }' –