[英]update data using jQuery - MVC
I am working on a MVC application and I have a ActionLink
for edit
which turns the specific row into textboxes and save
ActionLink
appears instead of Edit
on that row, but when I make changes and click on save
data isn't saved in database, I just see the old data. 我正在MVC应用程序上工作,我有一个
ActionLink
进行edit
,它将特定的行转换为文本框并save
ActionLink
而不是在该行上显示Edit
,但是当我进行更改并单击“ save
数据”时,我没有将其保存在数据库中,只是看到旧数据。
jQuery Code: 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 Code: 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>
Controller Code for edit: 控制器代码进行编辑:
[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);
}
Can anyone tell me where I am mistaking or where I have to make changes?? 谁能告诉我我在哪里犯错或必须在哪里进行更改?
From comment: You are making a GET
request to the server using .load
. 注释来自:您正在使用
.load
向服务器发出GET
请求。 That will not send any changes. 那不会发送任何更改。 You need to send the changes instead using an
$.ajax
POST
request (with the appropriate data
property sent along with it). 您需要使用
$.ajax
POST
请求发送更改(同时发送适当的data
属性)。
eg something like this (not exact, but give you the idea): 例如,像这样的东西(不精确,但是给你个主意):
$('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)
});
});
});
Another problem is with your view. 另一个问题是您的观点。 You cannot render a form with repeated items using a
foreach
loop. 您不能使用
foreach
循环渲染具有重复项的表单。 That does not provide enough information for the EditorFor
type methods to render an index (which allows it to name the items uniquely) . 这没有为
EditorFor
类型方法提供足够的信息来呈现索引(这使索引无法唯一地命名项目)。 You need to use a simple for
loop: 您需要使用一个简单的
for
循环:
eg 例如
for (int i = 0; i < Mode.Length; i++){
{
@Html.TextBoxFor(modelItem => Model[i].Holiday_Id, new { style = "display: none; width:170px; height:15px" })
[snip]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.