简体   繁体   English

在 ASP.NET Core MVC 的 iframe 中显示局部视图

[英]Display partial view in an iframe in ASP.NET Core MVC

I've been trying for some time to render a razor page in an IFrame in ASP.NET Core MVC 2.2.我一直在尝试在 ASP.NET Core MVC 2.2 中的 IFrame 中呈现剃刀页面。

I have my main page and some menus that should open specific pages in my IFrame.我有我的主页和一些应该在我的 IFrame 中打开特定页面的菜单。

The action that opens my main screen is this.打开我的主屏幕的动作是这样的。

[HttpGet]
public IActionResult IndexAdm()
{
    try
    {
        return View();
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

Here is the code that loads the partial view on my screen.这是在我的屏幕上加载局部视图的代码。

public PartialViewResult IframeAction(string sortBy)
{
    if (sortBy == null)
        return PartialView("../Company/ViewPartial");
    else
    {
        return PartialView(sortBy);
    }
}

The first time my page loads, everything works fine.我的页面第一次加载时,一切正常。 The main page is loaded and the other page is loaded into my iframe, as shown in the image below.主页已加载,另一个页面已加载到我的 iframe 中,如下图所示。

这是我想要得到的结果的图像

What I want to get is what happens above, but when I click my menu button, the main page disappears and only my partial page is loaded in the browser, as you can see in the image below.我想要得到的是上面发生的事情,但是当我单击菜单按钮时,主页消失了,浏览器中只加载了我的部分页面,如下图所示。

单击菜单项时看起来像这样

My menu item code is low.我的菜单项代码很低。

<li>
    <a class="nav-link" href="#">
        <i class="fas fa-question"></i>
        @Html.ActionLink("FAQ", "IframeAction", "Company", new { sortBy = "../Company/ViewPartial" }, null)@*https://www.codeproject.com/Questions/1221703/How-do-I-update-an-iframe-using-ASP-NET-core*@
    </a>
</li>

My IFrame code is below.我的 IFrame 代码如下。

<iframe id="IframeAdm" name="IFrameAdm" width="100%" height="85%" border="none" src="/Company/IframeAction"></iframe>

How can I make my partial page render inside my iframe without my main page disappearing?如何在我的 iframe 中呈现我的部分页面而我的主页不消失?

The @Html.ActionLink will create an <a> tag which will work as a link to redirect to the mentioned page. @Html.ActionLink将创建一个<a>标记,该标记将用作重定向到提到的页面的链接。 Based on your use case, it looks like you may want to leverage aspnet core's layout feature and maybe partial views.根据您的用例,您似乎想要利用 aspnet 核心的布局功能和部分视图。 You can check this link .您可以查看此链接

If you must however update an iframe, you will need to use javascript to update the src of the iFrame.但是,如果您必须更新 iframe,则需要使用 javascript 更新 iFrame 的 src。 Since you are using .net core 2.2, jquery will already be imported.由于您使用的是 .net core 2.2,jquery 已经被导入。 You can use the following jquery您可以使用以下jquery

function navigate(path) {
    $("#IframeAdm").attr('src', path);
}

Now, in your menu items, you can do something like this:现在,在您的菜单项中,您可以执行以下操作:

<li>
    <a class="nav-link" href="#" onclick="navigate(@Url.Content("your/path/here"))">
        <i class="fas fa-question"></i>
    </a>
</li>

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

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