ASP.net mvc 页脚常用数据

发布于 2024-07-22 05:35:18 字数 159 浏览 10 评论 0原文

所以,我有一个页脚,它将出现在我的网络应用程序的每个页面上,

我需要它来呈现动态数据,所以......这意味着每个控制器操作都需要返回包含此数据的viewData......以及特定于操作的viewData data

你们会如何实现这个? 也许是基本控制器的构造函数?

So, I have a footer which will appear on every page of my web application

I need it to render dynamic data so.... this means that every controller action needs to return viewData which contains this data.. as well as the action specific data

How would you guys implement this? The constructor of a base controller perhaps?

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

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

发布评论

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

评论(5

猫瑾少女 2024-07-29 05:35:18

您的基本控制器想法是正确的,但我会重写 OnActionExecuted 并在那里生成数据。 在生成公共数据之前检查结果是否为 ViewResult。 如果结果是重定向或通过 AJAX 返回的数据,则无需生成数据。

您还可以考虑为公共数据(如果数据很广泛)创建一个仅查看模型,并将其作为一个整体放入 ViewData 中。 然后,您可以创建一个强类型的分部视图,该视图采用模型并在视图中更轻松地使用模型的属性。 从母版页呈现此部分视图可以轻松合并每个页面上的数据并以强类型的方式使用它。

如果页脚数据或格式并不复杂,那么将标记放在主页中可能会更好。

You're on the right track with the base controller idea, but I would override OnActionExecuted and generate the data there. Check to see if the result is going to be a ViewResult before generating the common data. There's no need to generate the data if the result is a redirect or data going back via AJAX.

You might also consider creating a view-only model for the common data (if the data is extensive) and putting it in ViewData as a whole. Then you can create a strongly-typed partial view that takes the model and use the model's properties more easily in the view. Rendering this partial view from the master page would make it easy to both incorporate the data on every page and use it in a strongly-typed way.

If the footer data or formatting isn't complicated, then just putting the mark up in the master page is probably better.

椒妓 2024-07-29 05:35:18

您可以创建一个 ActionFilter 将数据写入 ViewData,如下所示:

public class CustomActionFilter : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        filterContext.Controller.ViewData["WillNeedThis"] = "Foo";
    }
}

然后装饰需要该数据的控制器和/或操作:

[CustomActionFilter]
public class HomeController : Controller {
   ...
}

You can create an ActionFilter that writes the data into the ViewData like so :

public class CustomActionFilter : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        filterContext.Controller.ViewData["WillNeedThis"] = "Foo";
    }
}

Then decorate your controllers and/or actions that's gonna need that data :

[CustomActionFilter]
public class HomeController : Controller {
   ...
}
晒暮凉 2024-07-29 05:35:18

您可以将数据传递到母版页并在那里显示页脚:

在 ASP.NET MVC 中将数据传递到母版页

You could pass data to a master page and show a footer there:

Passing data to Master Page in ASP.NET MVC

甜柠檬 2024-07-29 05:35:18

使用 MVC Futures 中的 Html.RenderAction() 方法怎么样?集会?

-- Site.master --

  <div id="footer">
        <% Html.RenderAction("SomeAction", "SomeController",
               new { someParam = ViewData["someValue"] }); %>
  </div>

有些人可能会认为这会混淆视图和控制器分离的水域,我可能不会在 ViewPage 中这样做,但我认为它是一个不错的替代方案主视图页面。

火焰燃烧;)

What about using the Html.RenderAction() method from the MVC Futures assembly?

-- Site.master --

  <div id="footer">
        <% Html.RenderAction("SomeAction", "SomeController",
               new { someParam = ViewData["someValue"] }); %>
  </div>

Some might argue that this muddies the waters of View and Controller separation, and I probably wouldn't do this in a ViewPage, but I see it as a decent alternative in a MasterViewPage.

Flame On ;)

长不大的小祸害 2024-07-29 05:35:18

如果数据不复杂,我使用 ViewData 字典来存储此信息。 如果是,我将页脚数据放入所有模型的基类中,然后将页脚移动到 ContentPlaceholder 中。 这有点烦人,因为您必须将其放在每个视图页面上。

If the data isn't complex I use the ViewData dictionary to store this information. If it is, I put the footer data in a base class for all models, then move the footer into a ContentPlaceholder. This is a little more annoying as you have to put it on every View page.

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