mvc3 - 在不同区域使用部分视图

发布于 2024-12-03 20:37:20 字数 440 浏览 2 评论 0原文

我有两个关于部分视图的问题...

  1. 何时使用部分视图与 @helper 方法,我都使用过 可以互换,并且希望在他们的 用法。你们是做什么的?

  2. 如何引用另一个区域的局部视图。

我有一个名为 admin 的区域,并且在常规 Views 目录中有部分视图。我如何使用它..我已经尝试了以下方法,但由于找不到它而不起作用。

@Html.Partial(VirtualPathUtility.ToAbsolute("~/Views/ControllerName/_PartialView"),
 Model)

其他我已经尝试过 -

@Html.Partial("~/Views/ControllerName/_PartialView", Model)

I have two questions regarding partial views...

  1. When to use Partial views vs @helper methods, i have used both
    interchangeably and would like to get more consistent in their
    usage. What do you guys do?

  2. How do you reference a partial view from another area.

I have an area called admin and i have a partial view in the regular Views directory. How do i use it .. i have tried the following which dont work as it cant be found.

@Html.Partial(VirtualPathUtility.ToAbsolute("~/Views/ControllerName/_PartialView"),
 Model)

other i have tried -

@Html.Partial("~/Views/ControllerName/_PartialView", Model)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

挽心 2024-12-10 20:37:20

当你说“助手”时,我不确定你指的是 Html 助手还是剃刀助手。无论如何,我只在它是像控件这样的小型单独项目时创建 Html 助手。

如果您指的是 Razor 助手,那么它们与 Partials 不同,因为您可以像函数一样调用它们,传递您想要的任何参数。部分很大程度上取决于“模型”系统(当然还有 Temp/ViewData/Bag。

这完全取决于您希望如何使用代码。

至于您的部分。您必须包含后缀。

@Html.Partial("~/Views/ControllerName/_PartialView.cshtml", Model)

I'm not sure if you mean Html helpers, or razor helpers when you say "helpers" In any case, I only create Html helpers when it's a small, idividual item like a control.

If you mean Razor helpers, then they are different from Partials in that you can call them like functions, passing whatever parameters you want. Partials are largely stuck with the "model" system (and of course Temp/ViewData/Bag.

It's all about how you want to work with the code.

As for your Partial. You have to include the suffix.

@Html.Partial("~/Views/ControllerName/_PartialView.cshtml", Model)
向日葵 2024-12-10 20:37:20

由于提问者询问了区域,因此这里介绍了如何在区域中进行操作

 @Html.Partial("~/Areas/Store/Views/Pages/Checkout.cshtml")

Since the questioner asked about areas here's how to do it in an area

 @Html.Partial("~/Areas/Store/Views/Pages/Checkout.cshtml")
心凉 2024-12-10 20:37:20

我只是给出我正在尝试做的事情的具体而简单的例子。
我需要能够使用位于主共享文件夹中的部分视图从区域页面注销。这就是我所做的:

  1. 在区域视图中,我通过以下方式引用部分视图

     
    ; <部分 id="登录"> **@Html.Partial("~/Views/Shared/_LoginPartial.cshtml")**
  2. 在区域视图中,我通过

    _LoginPartial 代码所在的主共享文件夹中 我添加了 {new = area (" ")},来自:

    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    

    至:

    using (Html.BeginForm("LogOff", "Account", **new { area = "" },** FormMethod.Post, new { id = "logoutForm", @class = "navbar-正确的” }))
    

Hope这在某种程度上有帮助!

I am just giving specific and simple example of what I'm trying to do.
I need to be able to logoff from an area page using the partialview located in the main shared folder. Here's what I did:

  1. At the area view I reference the partial view by

       <div class="float-right">
            <section id="login">            
              **@Html.Partial("~/Views/Shared/_LoginPartial.cshtml")**
            </section>
       </div>
    
  2. At the Main shared folder where the _LoginPartial code was located I added {new = area ("")}, from:

    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    

    to:

    using (Html.BeginForm("LogOff", "Account", **new { area = "" },** FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    

Hope that helps in some way!

水水月牙 2024-12-10 20:37:20

另一种选择是使您想要在共享区域之间共享的部分视图。

所以你把它放在主 ~/Views/Shared/ 文件夹中,例如

~/Views/Shared/_MyPartialView.cshtml.

你可以通过说从任何区域引用它

@Html.Partial("_MyPartialView")

Another option is to make the partial view you want to share between areas SHARED.

So you put it in the main ~/Views/Shared/ folder, e.g.

~/Views/Shared/_MyPartialView.cshtml.

You can then refer to it from any area by saying

@Html.Partial("_MyPartialView")
峩卟喜欢 2024-12-10 20:37:20

确保区域中的控制器具有 [Area("MyArea")] 注释。截至这篇文章,通过 ASP.NET Core 中的 Ajax div 更新从跨区域边界拉取部分视图对我来说适用于标签帮助器和 @Html.ActionLink。

Make sure your Controllers in Areas have the [Area("MyArea")] annotation. As of this post, pulling in Partial Views from across Area boundries via Ajax div updates in ASP.NET Core works for me with Tag Helpers and @Html.ActionLink.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文