Skip to content Skip to sidebar Skip to footer

Is It Possible To Call A Modal Popup (javascript) In Mvc Controller Return

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

Solution 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">
        <ahref="@Url.Action("NotFound", "Name")" id="NotFound"class="btn btn-primary"></div><divclass="modal fade"id="NotFound-Model"tabindex="-1"role="dialog"aria-labelledby="myModalLabel"aria-hidden="true"><divclass="modal-dialog"><divclass="modal-content"><divclass="modal-header"><buttontype="button"class="close"data-dismiss="modal"aria-label="Close"><spanaria-hidden="true">&times;</span></button><h4class="modal-title">Add Holiday</h4></div><divclass="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

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

Hope that helps!

Post a Comment for "Is It Possible To Call A Modal Popup (javascript) In Mvc Controller Return"