我是新来的MVC,我想编辑的行和编辑数据发送到通过jQuery和AJAX控制器的方法,当我点击修改
的特定行变成文本框和保存
ActionLink的,而不是出现修改
的,但是当我将它保存它给了我一个例外,因为空是怎么回事在数据
I am new to MVC, I am trying to edit a row and sending edited data to a controller method via jQuery and AJAX, when I click on edit
the specific row becomes textboxes and save
ActionLink appears instead of edit
, but when I save it it gives me an exception because null is going in data
jQuery的/阿贾克斯code:
jQuery/Ajax Code:
$(document).ready(function () {
function toggleEditability() {
$(this).closest('tr')
.find('a.Edit, a.Save, .displayText, input[type=text]')
}
$('a.Edit').click(toggleEditability);
$('a.Save').click(function () {
toggleEditability.call(this);
var data = $(this).closest('tr').serialize();
alert(data);
//var url = $(this).attr('href');
var actionURL = '@Url.Action("Edit", "Holiday")';
$.ajax({
url: actionURL, // 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) {
$("#dialog-edit").dialog('open');
}
});
});
});
警报(数据)
将显示有关信息
alert(data)
is showing this
CHTML code:
chtml code:
<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>
@* <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>
<div class="HolidayName">
@Html.TextBoxFor(modelItem => item.Holiday_Name, new { id = "", 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 = "", 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 = "", 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>
控制器方法code:
Controller Method Code:
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);
}
tbl_HolidayList.cs
tbl_HolidayList.cs
namespace OTESSystem.Models
{
using System;
using System.Collections.Generic;
public partial class tbl_HolidayList
{
public int Holiday_Id { get; set; }
public string Holiday_Name { get; set; }
public string Holiday_Description { get; set; }
public Nullable<System.DateTime> Holiday_date { get; set; }
}
}
你能告诉我怎么走数据文本框的值
的jQuery救呢?
Can you tell me how to get the textboxes values in data
for jQuery to save it?
异常和数据的方法来了空:
exception and data coming null in method:
你得到空
为警报(数据)
因为你想序列化最接近 TR
的全部内容,它包含非输入控制为好。因此,尝试像下面序列化仅仅是输入控件
You're getting null
for alert(data)
because you're trying to serialize the entire content of the closest tr
and it contains non-input controls as well. So try to serialize just the input controls like below
var data = $(this).closest('tr').find('input').serialize();
alert(data);
和你的警告,你应该返回 TR
数据作为图像如下图所示。
and your alert should return you the tr
data as shown below in the image.
如果您的AJAX发布模型数据返回空
在保存
行动,那么另一种是建立你的数据从数组序列化的数据对象,只需要你有什么需要你的 $('a.Save)。点击(函数内类似下面(所有低于code(){...
)
If your AJAX posted model data returns null
at the Save
action then an alternate would be to build your data array from the serialized data object and take only what you would require something like below (all the below code within your $('a.Save').click(function () { ...
)
var dataArray = $(this).closest('tr').find('input').serialize();
dataObj = {};
// loop through and create your own data object
$(dataArray).each(function (i, field) {
dataObj[field.name] = field.value;
});
var data = {};
data.Holiday_Id = dataObj['item.Holiday_Id'];
data.Holiday_Name = dataObj['item.Holiday_Name'];
data.Holiday_Description = dataObj['item.Holiday_Description'];
data.Holiday_date = dataObj['item.Holiday_date'];
var actionURL = '@Url.Action("Save", "Holiday")';
$.ajax({
url: actionURL, // Url to send request to
data: JSON.stringify({ model: 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) {
$("#dialog-edit").dialog('open');
}
});
现在你应该可以看到在发布的数据控制器的保存
操作。
更新1:
根据在您的评论指出错误,好像你的主键是未来的空
到控制器的操作方法。您可以添加一个隐藏的输入字段的标记,如下图所示。
UPDATE 1:
Based on the error noted in your comment it seems like your primary key is coming as null
to the controller's action method. You can add a hidden input field to the markup as shown below.
.....
<td>
@Html.DisplayFor(modelItem => item.Holiday_Id)
@Html.HiddenFor(modelItem => item.Holiday_Id);
</td>
....