2015-01-08 184 views
-1

我在MVC新的和我的工作項目中,我有一個GridView顯示數據,當我點擊Edit特定行變得textboxesEdit成爲Save,我編輯數據,然後點擊Save,控制權就在jQuery的功能,編輯的行數據發送到使用AJAX控制方法,我把警報看到塔的數據,給了我這個字符串enter image description here檢索信息

和空是要我控制器方法和我得到一個異常enter image description here

我的jQuery/Ajax代碼:

<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 data = $(this).closest('tr').find('input').serialize();  
       alert(data);   
       $.ajax({ 
        url: '@Url.Action("Edit", "Holiday")',    // 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 
        //contentType: 'application/json; charset=utf-8', 
        success: function (html) { 
         alert(html); 
         console.log(html); 
        }, 
        error: function() { 
         alert("error"); 
        } 
       }); 
      }); 
     }); 
    </script> 

CHTML代碼:

<div id="details"> 
    <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> 
       @Html.HiddenFor(modelItem => item.Holiday_Id) 
      @*<td> 
       @Html.DisplayFor(modelItem => item.Holiday_Id) 
       @Html.HiddenFor(modelItem => item.Holiday_Id) 
      </td>*@ 
      <td> 
       <div class="HolidayName"> 
        @Html.TextBoxFor(modelItem => item.Holiday_Name, new { id = "txtname", name="HolidayName", style = "display: none; width:170px; height:15px" }) 
       </div> 
       <div class="displaytext"> 
        @Html.DisplayFor(modelItem => item.Holiday_Name) 
       </div> 
      </td> 
      <td> 
       <div class="HolidayDescription"> 
        @Html.TextBoxFor(modelItem => item.Holiday_Description, new { id = "txtdesc", name="HolidayDescription", style = "display: none; width:170px; height:15px" }) 
       </div> 
       <div class="displaytext"> 
        @Html.DisplayFor(modelItem => item.Holiday_Description) 
       </div> 
      </td> 
      <td> 
       <div class="HolidayDate"> 
        @Html.TextBoxFor(modelItem => item.Holiday_date, new { id = "txtdate", name="HolidayDate", style = "display: none; width:170px; height:15px" }) 
       </div> 
       <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> 

    </div> 

和我的控制器方法的代碼是:

[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();//save changes 
       TempData["Msg"] = "Data has been updated succeessfully"; 
       return RedirectToAction("Index"); 
      } 
      return PartialView(tbl_holidaylist); 
     } 

我的問題是,我如何才能找回我從該字符串的具體信息在data如此我的控制器方法可以得到這個參數?

+0

可能重複(HTTP:// stackoverflow.com/questions/27806182/getting-multiple-textboxes-values-in-jquery-mvc) –

+0

這個問題是不同的,這個問題是關於格式化字符串 –

回答

-1

我做了這些改變其固定我的問題[jQuery中獲得多個文本框的值 - MVC]的

1

您的控件名稱與我們的屬性名稱不匹配(例如,您的tbl_holidaylist的屬性名稱爲Holiday_Name,但您發佈的關鍵值爲item.Holiday_Name)。您也正在生成無效的HTML(每行重複id屬性)。取而代之的

var data = $(this).closest('tr').find('input').serialize(); 

使用

var inputs = $(this).closest('tr').find('input'); 
var id = inputs.eq(0).val(); 
var name = inputs.eq(1).val(); 
var description = inputs.eq(2).val(); 
var date = inputs.eq(3).val(); 
var data = { Holiday_Id: id, Holiday_Name: name, Holiday_Description: description, Holiday_date: date }; 
.... 

我也建議你從

@Html.TextBoxFor(modelItem => item.Holiday_Name, new { id = "txtname", name="HolidayName", style = "display: none; width:170px; height:15px" }) 

改變@Html.TextBoxFor()方法HTML屬性

@Html.TextBoxFor(modelItem => item.Holiday_Name, new { id = "" }) 

這將刪除id屬性。然後用CSS來處理的風格,而不是內聯格式化

table input { 
    display: none; 
    width:170px; 
    height:15px; 
}