asp.net-mvc 如何在视图中加载局部视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7295835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:26:47  来源:igfitidea点击:

How can i load Partial view inside the view

asp.net-mvcasp.net-mvc-3razorasp.net-mvc-partialview

提问by patel.milanb

I am very confuse with this partial view...

我对这种片面的看法感到非常困惑......

I want to load a partial view inside my main view ...

我想在我的主视图中加载一个局部视图......

here is the simple exmaple...

这是一个简单的例子......

I am loading Index.cshtml of the Homecontroller Index action as a main page..

我正在加载 Homecontroller Index 操作的 Index.cshtml 作为主页..

in index.cshtml I am creating a link via

在 index.cshtml 我通过创建一个链接

@Html.ActionLink("load partial view","Load","Home")

in HomeController I am adding a new Action called

在 HomeController 中,我添加了一个名为的新操作

public PartialViewResult Load()
{
    return PartialView("_LoadView");
}

in _LoadView.cshmtl I am just having a

在 _LoadView.cshmtl 我只是有一个

<div>
    Welcome !!
</div>

BUT, when run the project, index.cshtml renders first and shows me the link "Load Partial View" ... when I click on that it goes to new page instade of rendering the welcome message from _LoadView.cshtml into the index.cshtml.

但是,当运行项目时,index.cshtml 首先呈现并向我显示链接“加载部分视图”......当我点击它时,它会进入新页面,将 _LoadView.cshtml 中的欢迎消息呈现到 index.cshtml 中.

What can be wrong?

有什么问题?

Note: I don't want to load page through AJAX or don't want to use Ajax.ActionLink

注意:我不想通过 AJAX 加载页面或不想使用 Ajax.ActionLink

回答by Darin Dimitrov

If you want to load the partial view directly inside the main view you could use the Html.Actionhelper:

如果你想直接在主视图中加载局部视图,你可以使用Html.Action帮助器:

@Html.Action("Load", "Home")

or if you don't want to go through the Load action use the HtmlPartialAsync helper:

或者,如果您不想执行 Load 操作,请使用 HtmlPartialAsync 帮助程序:

@await Html.PartialAsync("_LoadView")

If you want to use Ajax.ActionLink, replace your Html.ActionLinkwith:

如果您想使用Ajax.ActionLink,请将您的替换Html.ActionLink为:

@Ajax.ActionLink(
    "load partial view", 
    "Load", 
    "Home", 
    new AjaxOptions { UpdateTargetId = "result" }
)

and of course you need to include a holder in your page where the partial will be displayed:

当然,您需要在页面中包含一个持有人,其中将显示部分内容:

<div id="result"></div>

Also don't forget to include:

也不要忘记包括:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

in your main view in order to enable Ajax.*helpers. And make sure that unobtrusive javascript is enabled in your web.config (it should be by default):

在您的主视图中以启用Ajax.*助手。并确保在您的 web.config 中启用了不显眼的 javascript(默认情况下应该是这样):

<add key="UnobtrusiveJavaScriptEnabled" value="true" />

回答by Scott

I had exactly the same problem as Leniel. I tried fixes suggested here and a dozen other places. The thing that finally worked for me was simply adding

我和Leniel遇到了完全相同的问题。我尝试了这里和其他十几个地方建议的修复。最终对我有用的东西只是添加

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")

to my layout...

到我的布局...

回答by Stefan Vlad

For me this worked after I downloaded AJAX Unobtrusive library via NuGet :

对我来说,这在我通过 NuGet 下载 AJAX Unobtrusive 库后起作用了:

 Search and install via NuGet Packages:   Microsoft.jQuery.Unobtrusive.Ajax

Than add in the view the references to jquery and AJAX Unobtrusive:

然后在视图中添加对 jquery 和 AJAX Unobtrusive 的引用:

@Scripts.Render("~/bundles/jquery")
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"> </script>

Next the Ajax ActionLinkand the divwere we want to render the results:

接下来是我们想要呈现结果的 Ajax ActionLinkdiv

@Ajax.ActionLink(
    "Click Here to Load the Partial View", 
    "ActionName", 
    null, 
    new AjaxOptions { UpdateTargetId = "toUpdate" }
)

<div id="toUpdate"></div>

回答by Major Byte

If you do it with a @Html.ActionLink()then loading the PartialView is handled as a normal request when clicking a anchor-element, i.e. load new page with the reponse of the PartialViewResult method.
If you want to load it immedialty, then you use @Html.RenderPartial("_LoadView")or @Html.RenderAction("Load").
If you want to do it upon userinteraction (i.e. clicking a link) then you need to use AJAX --> @Ajax.ActionLink()

如果您使用 a 来完成,@Html.ActionLink()则在单击锚元素时加载 PartialView 将作为正常请求处理,即使用 PartialViewResult 方法的响应加载新页面。
如果您想立即加载它,那么您可以使用@Html.RenderPartial("_LoadView")@Html.RenderAction("Load")
如果您想在用户交互(即单击链接)时执行此操作,则需要使用 AJAX -->@Ajax.ActionLink()

回答by 3d cad consultant

Small tweek to the above

小tweek到上面

@Ajax.ActionLink(
    "Click Here to Load the Partial View", 
    "ActionName", 
    "ControlerName",
    null, 
    new AjaxOptions { UpdateTargetId = "toUpdate" }
)

<div id="toUpdate"></div>

回答by maxspan

RenderParital is Better to use for performance.

RenderParital 更适合用于性能。

{@Html.RenderPartial("_LoadView");}

回答by Jaimin Dave

if you want to populate contents of your partial view inside your view you can use

如果你想在你的视图中填充你的局部视图的内容,你可以使用

@Html.Partial("PartialViewName")

or

或者

{@Html.RenderPartial("PartialViewName");}

if you want to make server request and process the data and then return partial view to you main view filled with that data you can use

如果您想发出服务器请求并处理数据,然后将部分视图返回到填充了该数据的主视图,您可以使用

...
    @Html.Action("Load", "Home")
...

public PartialViewResult Load()
{
    return PartialView("_LoadView");
}

if you want user to click on the link and then populate the data of partial view you can use:

如果您希望用户单击链接然后填充部分视图的数据,您可以使用:

@Ajax.ActionLink(
    "Click Here to Load the Partial View", 
    "ActionName", 
    "ControlerName",
    null, 
    new AjaxOptions { UpdateTargetId = "toUpdate" }
)