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

给你一囗甜甜゛ 提交于 2019-12-24 20:49:40

问题


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");
}

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


回答1:


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

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 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');
            });
    }

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

Hope that helps!



来源:https://stackoverflow.com/questions/52501048/is-it-possible-to-call-a-modal-popup-javascript-in-mvc-controller-return

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!