【问题标题】:Ajax edit ASP.NET MVCAjax 编辑 asp.net mvc
【发布时间】:2021-03-07 17:37:54
【问题描述】:

如果我将 AJAXEditIsDoneCheckBox 更改为 Edit,代码将不起作用,这是为什么?代码无需更改即可正常工作,但我想知道问题所在。我将不胜感激。

控制器

[HttpPost]
        public ActionResult AJAXEditIsDoneCheckBox(int? id, bool value)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ToDo toDo = db.ToDos.Find(id);
            if (toDo == null)
            {
                return HttpNotFound();
            }
            else
            {
                toDo.IsDone = value;
                db.Entry(toDo).State = EntityState.Modified;
                db.SaveChanges();
                return PartialView("_ToDoTable", GetToDoes());
            }
        }

脚本

$(document).ready(function () {
    $('.ActiveCheck').change(function () {
        var self = $(this);
        var id = self.attr('id');
        var value = self.prop('checked');

        $.ajax({

            url: '/ToDoes/EditIsDoneCheckBox',
            data: {
                id: id,
                value: value
            },
            type: 'POST',
            success: function (result) {
                $('#tableDiv').html(result);
            }
        });
    });

});

查看

 @Html.CheckBoxFor(modelItem => item.IsDone, new { id = item.Id, @class = "ActiveCheck" })

【问题讨论】:

  • “代码不工作” - 哪个部分不工作?

标签: c# ajax asp.net-mvc


【解决方案1】:

如果您将控制器中的操作名称从AJAXEditIsDoneCheckBox 更改为Edit,那么您还必须更新ajax 方法中的URL:

// controller
[HttpPost]
public ActionResult Edit(int? id, bool value)
{ 
    ...
}

// view
$.ajax({
    url: '/ToDoes/Edit',
    ...
}

我希望我已经理解了这个问题,如果没有,请更新您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    • 2011-07-04
    • 1970-01-01
    相关资源
    最近更新 更多