选择/取消选择该行中的复选框时,我无法更新表格行中的文本。我希望它将 stye 从 text-decoration:none 更改为 text-decoration:line-through
我有一个部分视图,它呈现如下表格:
|Checkbox|label text|delete link|
|Checkbox|label text|delete link|
|Checkbox|label text|delete link|
I have implemented some JQuery code to update a Bool in the model when the Checkbox is selected/deselected and this appears to work. 模型正在更新。我正在从控制器返回部分视图并尝试用 Ajax 结果替换表
这是我的部分观点:
<div id="taskList">
<table>
@for (int i = 0; i < Model.Tasks.Count; i++)
{
<tr id="tableRowTask@(Model.Tasks[i].Id)">
<td>
@Html.CheckBoxFor(m => m.Tasks[i].IsDone, new { data_id=Model.Tasks[i].Id })
</td>
<td>
@Html.Label(Model.Tasks[i].Title, new { disabled=true, style=Model.Tasks[i].IsDone? "text-decoration:line-through" : "text-decoration:none" })
</td>
<td>
@Html.ActionLink("Delete", "Delete", new { id = Model.Tasks[i].Id })
</td>
</tr>
}
</table>
</div>
这是我的javascript:
$("input[type=checkbox]").change(function () {
var data_id = $(this).data("id");
var taskListId = '#taskList';
var tableRowId = '#tableRowTask' + data_id;
$.ajax({
url: 'Home/Edit',
type: 'POST',
data: { id: data_id, isChecked: $(this).is(':checked') },
success: function (result) {
$(taskListId).replaceWith(result);
}
});
});
这是我的控制器操作:
[HttpGet]
public ActionResult Index()
{
TaskListModel model = new TaskListModel() { Tasks = _db.Tasks.OrderBy(i => i.EntryDateTime).ToList() };
if (Request.IsAjaxRequest())
{
return PartialView("_TaskList", model);
}
return View(model);
}
// Mark a task as complete
[HttpPost]
public ActionResult Edit(int id = 0, bool isChecked = false)
{
// database logic
TaskItem taskItem = _db.Tasks.Find(id);
if (TryUpdateModel(taskItem))
{
taskItem.IsDone = isChecked;
_db.SaveChanges();
}
return RedirectToAction("Index");
}
谢谢大家的时间!