引发MVC格式模式弹出一个按钮点击 [英] trigger modal popup in mvc form with a button click

查看:261
本文介绍了引发MVC格式模式弹出一个按钮点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC这个观点我在其中一个模型显示详细信息。
该模式弹出工作正常,直到我没有把它在形式上块。
现在,它只是显示张贴在弹出的回吧。

I have this view in mvc in which I display details for a model. The modal popup was working fine until I didn't put it in form block. Now its only posting back instead of displaying the popup.

这是我的看法:

@using App.Portal.WebUI.Controllers
@using MvcPaging
@model IPagedList<App.Models.Device>

    @{
        ViewBag.Title = "Manage Devices";
    }

<h2>Manage Devices</h2>
@Html.ActionLink("Add New Device", "Manage", "Handhelds", new { @class = "editUser btn btn-info" })
<button id="showInactive" class="btn btn-primary">Show Inactive Devices</button>
<br /><br />
@using (Ajax.BeginForm("Home", "Handhelds",
    new AjaxOptions {UpdateTargetId = "grid-list", HttpMethod = "get", LoadingElementId = "loading", OnBegin = "beginPaging", OnSuccess = "successPaging", OnFailure = "failurePaging"},
    new {id = "frm-search"}))
{
    <div class="input-append">
        <input class="span2" id="appendedInputButton" type="text" name="handheld" placeholder="Enter Text" />
        <button class="btn" type="submit">
            <i class="icon-search"></i>&nbsp;Search</button>
    </div>
    <br />
    <div id="grid-list">
        <div class="table-responsive">
            <table id="dataTable" class="table table-striped">
                <tr>
                    <td>No</td>
                    <td>Device ID</td>
                    <td>Serial Number</td>
                    <td>Options</td>
                </tr>
                @foreach (var device in Model)
                {
                    <tr>
                        <td>@device.DeviceID</td>
                        <td>@device.DeviceIDView</td>
                        <td>@device.DeviceSerialNumber</td>
                        <td>
                            @Html.ActionLink("Edit", "Manage", new {id = @device.DeviceID}, new {@class = "editUser btn btn-info"})
                            <button id="btnDeleteHandheld" class="deleteHandheld btn btn-danger" data-id="@device.DeviceID">Delete</button>
                        </td>
                    </tr>
                }
            </table>
        </div>
    </div>
}

<script type="text/javascript">
    $(document).ready(function () {
        $('button.btnAddDevice').click(function () {
            $('#addNewDevice').modal('show');
        });

        $('button.deleteHandheld').click(function () {
            $("#hfDeviceId").val($(this).data('id'));
            $('#deleteConfirm').modal('show');
        });

        $('button.deleteDeviceConfirm').click(function () {
            $.ajax({
                url: '@Url.Action("Delete", "Handhelds")',
                data: { deviceid: $("#hfDeviceId").val() },
                dataType: "json",
                type: 'POST',
                success: function (data) {
                    if (data === "OK") {
                        $('#deleteConfirm').modal('hide');
                        $('#deleteConfirmation').modal('show');
                        setTimeout(function () {
                            location.reload();
                        }, 3000);
                    }
                },
                error: function (textStatus, errorThrown) {
                    Success = false;//doesnt goes here
                }
            });
        });
    });

</script>

<div id="deleteConfirm" class="modal fade" data-backdrop="static" tabindex="-1" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <h4>Are you sure you want to remove the device?</h4>
                <button class="btn btn-success deleteDeviceConfirm">Yes</button>
                <button class="btn btn-danger" data-dismiss="modal">No</button>
                <input type="hidden" id="hfDeviceId" />
            </div>
        </div>
    </div>
</div>

<div id="deleteConfirmation" class="modal fade" data-backdrop="static" tabindex="-1" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <h4>Device removed</h4>
                <button class="btn btn-danger btn-block" data-dismiss="modal">OK</button>
            </div>
        </div>
    </div>
</div>

我需要的按钮,btnDeleteHandheld的ID来触发相关的模式弹出。
任何人都可以请给我一些建议什么,我需要改变吗?

I need the button with id of btnDeleteHandheld to trigger the associated modal popup. Can anyone pls give me some advice what do I need to change?

谢谢,Laziale

Thanks, Laziale

推荐答案

我觉得按钮使输入默认提交。以prevent后使用preventDefault像这样:

I think the button make an input submit by default. To prevent the post use the preventDefault like so :

   $('button.deleteHandheld').click(function (e) {
        e.preventDefault();
        $("#hfDeviceId").val($(this).data('id'));
        $('#deleteConfirm').modal('show');
    });

您也可以用这个代替你按钮:

You can also replace you button with this :

<input type="button" "id="btnDeleteHandheld" class="deleteHandheld btn btn-danger" data-id="@device.DeviceID">Delete</input>

这篇关于引发MVC格式模式弹出一个按钮点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆