是否可以在 MVC 控制器 return 中调用模态弹出窗口 (javascript)

Is it possible to call a modal popup (javascript) in MVC controller return

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

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

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

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

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

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 模态

    $(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

@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>
}

希望对您有所帮助!