简体   繁体   English

从_Layout调用时,无法将部分视图加载为模式

[英]Not able to load partial view as modal when called from _Layout

In my Core 2.0 project I am trying to load a partial view to modal div when called from _Layout.cshtml but nothing happens. 在我的Core 2.0项目中,当我从_Layout.cshtml调用时,我试图将部分视图加载到模式div中,但是什么也没有发生。 On click of Create New User I am trying to load partial view on modal popup. 在单击“ 创建新用户”时,我试图在模式弹出窗口上加载部分视图。 Below is code- 下面是代码-

//_Layout.cshtml //_Layout.cshtml

<li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a></li>
<li><a href="/" onclick="CreateUser()">Create New User</a></li>

//Index.cshtml of Home //首页的Index.cshtml

<div class="modal fade" id="AppointmentSchedule" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header btn-primary">
                <h5 class="modal-title" id="AppointmentModalLabel"></h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body" id="ScheduleAppointment"></div>
        </div>
    </div>
</div>

//Javascript //使用Javascript

function CreateUser() {
            var url = appSetting + "Home/CreateUser";
            $.ajax({
                type: "GET",
                contentType: "application/json",
                url: url,
                data: null,
                dataType: "html",
                async: false,
                success: function (data) {
                    $("#ScheduleAppointment").html('');
                    $("#ScheduleAppointment").html(data);
                    $("#AppointmentSchedule").modal('show');
                },
                error: function (result) {
                    $("#divScheduleAppointment").html('');
                }
            })
        }

//Home Controller //家庭控制器

  public ActionResult CreateUser()
      {
        return PartialView("_CreateUserHome");
      }

On debugging I realize that after Ajax success it calls Index action method of Home controller(that should not be) and may be it is causes page refresh and popup may get close. 在调试时,我意识到Ajax成功后会调用Home控制器的Index action方法(不应这样做),并且可能是导致页面刷新和弹出窗口关闭的原因。 But what is it's solution. 但是这是什么解决方案。

With your current code, when user clicks on the anchor tag, the browser do a normal link click behavior, which is navigating to the href attribute value of the clicked link element. 使用当前代码,当用户单击锚标记时,浏览器将执行正常的链接单击行为,即导航至被单击链接元素的href属性值。 If you want show the modal dialog instead of that, you should prevent this default behavior. 如果要显示模式对话框而不是显示该对话框,则应避免这种默认行为。

You may pass an event object to the CreateUser method 您可以将event对象传递给CreateUser方法

<a href="/" onclick="CreateUser(event)">Create New User</a>

and in your method, call the preventDefault method, which will stop the normal link click behavior(navigating to to the href value) 然后在您的方法中,调用preventDefault方法,该方法将停止正常的链接点击行为(导航至href值)

function CreateUser(e)
{
    e.preventDefault();

    var url = appSetting + "Home/CreateUser";
    $.ajax({
        type: "GET",
        url: url,
        success: function (data)
        {
            $("#ScheduleAppointment").html(data);
            $("#AppointmentSchedule").modal('show');
        },
        error: function (result)
        {
            $("#divScheduleAppointment").html('');
        }
    })
}

A suggestion: If it is not a navigational link, Consider using a button instead of an anchor tag. 一个建议:如果它不是导航链接,请考虑使用按钮而不是锚标记。

Quick solution is to add return false to your event handler: 快速解决方案是在事件处理程序中添加return false:

    <li><a href="/" onclick="CreateUser(); return false;">Create New User</a></li>

This will suppress default behavior. 这将抑制默认行为。

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

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