【问题标题】:Script to delete record from database doesn't work从数据库中删除记录的脚本不起作用
【发布时间】:2016-10-06 20:22:44
【问题描述】:

我想使用位于索引网页上的按钮删除记录。我用AJAX写了一些jQuery,但它不起作用。

这是我的控制器方法。当我使用通过 HttpGet Delete 方法在另一个页面上生成的表单调用它时,此方法可以正常工作。

[HttpPost]
[ActionName("Delete")]
[ValidateAntiForgeryToken]
public IActionResult DeleteConfirmed(int id)
{
    var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
    var training = _context.Trainings.Where(t => t.UserId == userId).FirstOrDefault(t => t.Id == id);
    if (training != null && ModelState.IsValid)
    {
        _context.Trainings.Remove(training);
        _context.SaveChanges();
        return RedirectToAction("Index");
    }
    return View();            
}
$(document).ready(function () {
    $(".js-delete").on("click", function () {
        var button = $(this);
        var trainingId = button.attr;
        if (confirm("Are you sure you want to delete this training?")) {
            $.ajax({ 
                url: "/Training/Delete/" + trainingId,
                type: "POST",
                contentType: "application/json",
                success: function () {
                    alert("ok");
                },
                error: function(){
                    alert("not ok");                        
            }
            });
        }
    });
});
<button data-customer-id="@training.Id" class="btn btn-link js-delete">Usuń</button>

我现在的情况是:弹出窗口,在我确认得到脚本生成的错误结果后,在控制台中我的状态为:400,输入:xhr,响应为:

http://localhost:6822/Training/Delete/function%20(%20name,%20value%20)%20%7Breturn%20access(%20this,%20jQuery.attr,%20name,%20value,%20arguments.length%20%3E%201%20);%7D

【问题讨论】:

  • 您进行 ajax 调用和 ajax 调用无法重定向,因此 return RedirectToAction("Index"); 没有任何意义。您的方法没有模型参数,因此ModelState.IsValid 没有意义(它始终有效)。要设置data-customer-id 的值,它需要是var trainingId = $(this).data('customer-id');,ajax 应该是url: "/Training/Delete/", data: { id: trainingId }, 并删除contentType: "application/json",
  • 移除 contentType: "application/json" 并尝试,因为您在这里没有使用任何 JSON 格式。如果需要,还可以添加 dataType: "text"data: { id: trainingId }url: "/Training/Delete/"
  • @StephenMuecke - 感谢您的帮助。我根据您的提示编写了代码。但是我认为我的路由有问题(因为在控制台中它调用 Training/Delete/ 并且没有通过它看起来像这样:' routes.MapRoute( name: "default", template: "{controller=Home}/{action =Index}/{id?}");' 所以 id 是可选的,我找到了如何使用 MVC5 中的默认属性配置它的方法,但问题是 UrlParameter.Optional 不起作用。我应该重新配置我的路由还是以不同的方式编写函数?
  • 它与路由无关 - 调试你的脚本 - 检查 var trainingId = button.attr; 的值(它必须是 var trainingId = $(this).data('customer-id');。但你的代码中没有其他任何意义。你想重定向当id 值存在并且记录已被删除(这将是 99.99% 或时间),所以不要使用 ajax - 它没有意义。
  • 在极少数情况下,恶意用户发布了无效的id,您将视图返回给客户端但甚至从未使用它 - 它只会降低您的应用程序的性能。然后是什么 - 您是否希望用户再次单击删除按钮以删除不再存在的记录?进行正常提交。

标签: jquery asp.net ajax asp.net-mvc


【解决方案1】:
http://localhost:6822/Training/Delete/function%20(%20name,%20value%20)%20%7Breturn%20access(%20this,%20jQuery.attr,%20name,%20value,%20arguments.length%20%3E%201%20);%7D

我认为 url 应该是这样的:

http://localhost:6822/Training/Delete/5 (since your action expects int value in parameter.)

你应该调试你的脚本,看看你在 trainingId 中得到了什么。

        var button = $(this);
        var trainingId = button.attr;

因为在这里你在参数中传递了下面的函数。 函数%20(%20name,%20value%20)%20%7Breturn%20access(%20this,%20jQuery.attr,%20name,%20value,%20arguments.length%20%3E%201%20);%7D

我认为你应该尝试:

 var trainingId = $(this).attr("data-customer-id")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多