簡體   English   中英

單擊按鈕以 mvc 形式觸發模態彈出窗口

[英]Trigger modal popup in mvc form with a button click

我在 mvc 中有這個視圖,我在其中顯示模型的詳細信息。 模態彈出窗口工作正常,直到我沒有把它放在表單塊中。 現在它只回發而不是顯示彈出窗口。

這是我的看法:

@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;//doesn't 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 的按鈕來觸發關聯的模式彈出窗口。 我需要改變什么?

我認為該button默認情況下會提交輸入。 為了防止帖子使用 preventDefault 像這樣:

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

你也可以用這個替換你的按鈕:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM