是否可以在MVC控制器返回中调用模式弹出窗口(javascript) [英] Is it possible to call a modal popup (javascript) in MVC controller return

查看:377
本文介绍了是否可以在MVC控制器返回中调用模式弹出窗口(javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在控制器的return方法中调用JavaScript方法(将Modal显示为弹出窗口).

I am wondering if it is possible to call a JavaScript method (which displays a Modal as a popup) in the return method of a controller.

string name = home.entityDetails.Name;
if (name == " " || name == null)
{
    return PartialView("NotFound");
}

在调用return PartialView("Not found");的地方,是否可以返回显示模式的JavaScript方法?

Where return PartialView("Not found"); is called, is it possible to return a JavaScript method that shows a modal?

推荐答案

处理此问题的最佳方法是在视图内使用Bootstrap模态和javascript.

Best way to handle this is using Bootstrap modals and javascript inside your view.

由于您使用的是部分视图,因此我假设您有另一个父视图,例如索引视图.您可以在父视图内使用javascript将html附加为模态,然后从父视图打开部分视图.这是一个相同的例子.

Since you're using Partial view, I assume that you have another parent view such as Index View. You can attach html for your modals using javascript inside parent view, and then open your Partial view from Parent view. Here is an example of the same.

Index.cshtml

<div class="container">
        <a href="@Url.Action("NotFound", "Name")" id="NotFound" class="btn btn-primary">
</div>

    <div class="modal fade" id="NotFound-Model" tabindex="-1" role="dialog" aria-    labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-    label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">Add Holiday</h4>
                </div>
                <div class="divForNotFound">
                </div>
            </div>
        </div>
    </div>

JAVASCRIPT处理Bootstrap模式

JAVASCRIPT to handle Bootstrap Modal

    $(document).ready(function () {
            $('#NotFound').click(function (event) {
                event.preventDefault();
                $.get(this.href, function (response) {
                   $('.divForNotFound').html(response);
               });
                $('#Add-NotFound').modal({
                    backdrop: 'static',
                }, 'show');
            });
    }

假设您拥有部分视图NotFound.cshtml

Assuming you have a partial view NotFound.cshtml

@model Name.NotFoundModel
using (Ajax.BeginForm("NotFound", "Name", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "div-record", OnSuccess = "$('.close').click()" }))
{
    <div class="modal-body">
        <table class="table-bordered table-responsive table table-striped">
            <tr class="col-lg-12">
                <th class="label-primary">
                    @Html.Label("NotFoundLabel")
                </th>
            </tr>
        </table>
    </div>
}

希望有帮助!

这篇关于是否可以在MVC控制器返回中调用模式弹出窗口(javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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