JQuery确认没有命中控制器中的动作方法

时间:2013-09-30 05:29:40

标签: jquery asp.net-mvc

您好我是MVC和jQuery的新手。 任何人都可以请指导我以下。 当我点击删除链接时,删除操作永远不会命中。

我的观点:

  <table id="lookupValuesDetailsTable" class="table table-bordered table-striped table-hover">
        <thead>
            <tr>
                <th>Value</th>
                <th>Message</th>
                <th>EffectiveDate</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.LookupValues)
            {            
                <tr>

                    <td>@Html.DisplayFor(m => item.Value)</td>
                    <td>@Html.DisplayFor(m => item.Message)</td>
                    <td>@Html.DisplayFor(m => item.EffectiveDate)</td>
                    @* <td>@Html.DisplayFor(m => item.EffectiveDateDateForSorting)</td>*@
                    <td>
                        @Html.ActionLink("Delete", "Delete", "LookupValues", new { area = "Admin", id = item.LookupValueKey }, new { @class = "deleteLink" })

                    </td>
                </tr>
            }

        </tbody>
    </table>

<div id="dialog-confirm" title="CONFIRMATION" class="modal-header">
    <div class="modal-body">
        <p>This item will be deleted. Are you sure?</p>
    </div>
</div>


@section Scripts {
<script type="text/javascript">
     $("#dialog-confirm").dialog({
        autoOpen: false,
        modal: true,
        resizable: false,
        height: 180,
    });

    $(".deleteLink").click(function (e) {
        e.preventDefault();
        var targetUrl = $(this).attr("href");

        $("#dialog-confirm").dialog({
            buttons: {
                "Confirm": function () {
                    window.location.href = targetUrl;
                },
                "Cancel": function () {
                    $(this).dialog("close");
                }
            }
        });

        $("#dialog-confirm").dialog("open");
    });

</script>

我的控制器(LookupValuesController):

 [HttpPost]
    public ActionResult Delete(Guid id)
    {
        var lookupValueDetails = adminService.GetLookupValues(id);
        var model = AddLookupValueMappings.ToModel(id, lookupValueDetails);
        return View();
    }

任何人都可以告诉我,我做错了什么。

1 个答案:

答案 0 :(得分:0)

首先删除e.Prevntdefault();然后尝试下面..我还没检查过它......

$('.delete').click(function () {
            var answer = confirm('Do you want to delete this record?');
            if (answer) {
                $.post(this.href, function () {
                    window.location.reload(); //Callback
                });
                return false;
            }
            return false;
        });

和控制器如下: -

@Html.ActionLink("Delete", "Delete", new { area = "Admin", id = item.LookupValueKey }, new { @class = "delete" })