通过JQuery中的@ Url.Action向控制器发送参数时出错

时间:2016-05-24 09:21:38

标签: jquery asp.net-mvc url-action

我正在开展JQuery项目,我尝试使用@Url.Action<button class="btn btn-white btn-sm demo1" data-id='@item.TeamID'>Delete</button> 向我的控制器发送一些参数。

HTML code:

    $(document).ready(function () {
        $('.demo1').click(function (event) {
            swal({
                title: "Are you sure?",
                text: "You will not be able to recover this team!",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "Yes, delete it!",
                closeOnConfirm: false
            }, function () {
                var data = event.data;
                var id = data.id;
                var url = '@Url.Action("Delete", "Teams", new { id = "__param__" })';
                window.location.href = url.replace('__param__', encodeURIComponent(id));

                swal("Deleted!", "Your team has been deleted.", "success");
            });
        });
    });

JQuery代码:

foreach

但是,未触发团队控制器中的Delete方法。我错过了什么吗?

更新 HTML按钮位于@foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.TeamName) </td> <td> @Html.DisplayFor(modelItem => item.TeamInits) </td> <td> @Html.ActionLink("Edit", "Edit", new { id = item.TeamID }, new { @class = "btn btn-white btn-sm" }) <button class="btn btn-white btn-sm demo1" data-id='@item.TeamID'>Delete</button> </td> </tr> } 循环内:

empty()

2 个答案:

答案 0 :(得分:1)

使用HTMLElement.dataset属性或.data()来阅读自定义data-*前缀属性值。

$(document).ready(function () {
    $('.demo1').click(function (event) {            
        var id = this.dataset.id; 
        //OR
        var id = $(this).data('id');

        //Rest of the code
        swal();
    });
});

答案 1 :(得分:0)

我通常会尝试下面的代码技巧。当这样的事情出错时。

在您的Global.asax.cs文件中添加以下代码并将调试器放入方法中,以便在发生异常时触发它。

protected void Application_Error(object sender, EventArgs e)
{ 
    Exception exception = Server.GetLastError();
    string ex = exception.Message; // Here you will get to know what is going wrong
}

异常消息或堆栈跟踪将提供足够的信息详细信息来修复错误。

即使它的请求来自Jquery / Razor Html页面错误,你也会有足够的信息。

希望这会有所帮助!!!