2014-12-31 3 views
0
내가 MVC 응용 프로그램 일하고

을 나는 텍스트 상자에 특정 행을 전환 editsaveActionLink에 대한 ActionLink 해당 행 대신 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

댓글에서 : 을 사용하여 서버에 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' 클래스와 잘 동작하지만 데이터가 편집 메소드에서 null이 될 것입니다. '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; 세트; } }' –