简体   繁体   English

将 razor 页面显示为模态弹出窗口

[英]Show razor page as modal popup

I'm trying to render a razor page as the content of a modal dialog:我正在尝试将 razor 页面呈现为模式对话框的内容:

This is the razor page这是 razor 页面

<div class="container">
    <div class="title modal " tabindex="-1" id="loginModal"
         data-keyboard="false" data-backdrop="static">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4>@IdentityResources.ResendEmailConfirmation</h4>
                    <button type="button" class="close" data-dismiss="modal">×</button>
                </div>
                <div class="modal-body">
                    <form  method="post">
                        <div class="form-group">
                            <input class="form-control" type="text" placeholder="Email" id="inputUserName" />
                        </div>
                        <div class="modal-footer">
                            <button type="submit" class="btn btn-primary col-md-2">@IdentityResources.Send</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $("#loginModal").modal('show');
    });

    $("#btnHideModal").click(function () {
        $("#loginModal").modal('hide');
    });
</script>

And later in another razor page I create a link to the page:后来在另一个 razor 页面中,我创建了一个指向该页面的链接:

<a asp-page="/Identity/_ResendConfirmationEmail">@IdentityResources.ResendEmailConfirmation</a>

Once it's clicked the razor page is displayed, but since it's action link, it's redirecting directly to the page, instead of rendering it over the current page.一旦点击 razor 页面就会显示出来,但由于它是操作链接,它直接重定向到页面,而不是在当前页面上呈现它。

How can I change this behavior?我怎样才能改变这种行为?

You need to call your action , using ajax .您需要使用ajax调用您的action so in the response you will get html of your view.因此,在响应中,您将获得 html 的视图。 but you need to return PartialView() , because if you return your view as View() then youy view will come with layout, so your page will display layout contents twice in a page.但是您需要返回PartialView() ,因为如果您将视图返回为View() ,那么您的视图将带有布局,因此您的页面将在一个页面中显示两次布局内容。

 // your controller

 public ActionResult _ResendConfirmationEmail()
 {
    return PartialView();
 }

Now call it using ajax as below.现在使用ajax调用它,如下所示。

  $.ajax({
        url: '@Url.Action("_ResendConfirmationEmail","Identity")',
        success: function (data) {
            $('#yourdivid').html(data);
        },
        error : function (error)
        { 
          // handle error code
        }

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM