2015-01-08 155 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; 
}