有什么方法可以为我的_Layout创建特殊的控制器吗? [英] Is there any way to create special controller for my _Layout?

查看:61
本文介绍了有什么方法可以为我的_Layout创建特殊的控制器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个网站,该网站在 View \ Shared 文件夹中有一个名为 _MainPage 的视图,其他视图将其用作我的网站的基本布局,因此我有一节在从数据库填充数据的视图中,实际上,此部分是站点的最新新闻,我应该在此部分中显示最新新闻,以简单的方式,我应该将其命名为 latestNews :

I am developing a website that has a View called _MainPage in View\Shared folder that other views use that for basic layout of my website, so I have a section in this view that populates data from database in fact this section is latest news of the site and I should show latest news in this section ,in simple way I should make section for example called latestNews :

<ul class="news-list">
    @RenderSection("latestNews",required:false)
</ul>

在每个视图中,我应该用剃刀填充这一部分,以确保数据来自相关控制器:

and in each views I should fill this section by razor that data came from relevant controller :

@foreach (var item in Model.News)
{
    <div>
        <p>@Html.Raw(item.Body)<br /><a href="#">ادامه مطلب</a></p>
    </div>
}

实际上,我在页面页脚的每个视图中都有最新消息.

in fact I have latest news in every views in footer of my pages.

现在我的问题是:如何为未在每个视图中执行这些例程的View( _MainPage )定义自定义控制器.有什么通用的方法可以做到这一点吗?

now my question is : How can define a custom controller for my View (_MainPage) that haven't do these routine in each view. Is there any generic way to accomplish that?

推荐答案

控制器类中的任何公共方法都是一种动作方法.可以通过Web浏览器中的URL或应用程序中的查看页面调用控制器类中的每个操作方法.

Any public method in a controller class is an action method. Every action method in a controller class can be invoked via an URL from web browser or a view page in application.

您的情况是一些动态信息(数据)需要显示在应用程序的几个页面上.通常,我们最后将数据添加到通过操作方法传递给视图的模型中.我们在多个模型中复制了相同的数据,并采用了DRY(不要自己重复)原理.

Your scenario is some dynamic information (data) need to displayed on couple pages in your application. Generally we end up in adding that data to model passed to views by action methods. We duplicate same data in multiple models where we brake DRY (Don’t Repeat Yourself) principle.

要处理这种情况,ASP.NET MVC提供了子操作方法.任何操作方法都可以是操作方法的子操作,但子操作是从视图内部调用的操作方法,不能通过用户请求(URL)调用操作子方法.

To handle this scenario ASP.NET MVC provides child action methods. Any action method can be child an action method but child actions are action methods invoked from within a view, you can not invoke child action method via user request (URL).

我们可以使用 [ChildActionOnly] 属性为操作方法添加注释,以创建子操作.通常,我们使用带有部分视图的子操作方法,但并非每次都使用.

We can annotate an action method with [ChildActionOnly] attribute for creating a child action. Normally we use child action methods with partial views, but not every time.

[ChildActionOnly] 表示一个属性,该属性用于指示仅应将操作方法​​作为子操作调用.

[ChildActionOnly] represents an attribute that is used to indicate that an action method should be called only as a child action.

[ChildActionOnly]
public ActionResult GetNews(string category)
{
    var newsProvider = new NewsProvider();
    var news = newsProvider.GetNews(category);
    return View(news);
}

可以使用 Html.RenderAction() Html.Action()方法在应用程序的任何视图内调用上述子操作方法.

Above child action method can be invoked inside any view in the application using Html.RenderAction() or Html.Action() method.

@Html.Action("GetNews", "Home", new { category = "Sports"})

(或)

@{ Html.RenderAction("GetNews", "Home", new { category = "finance"}); }

这篇关于有什么方法可以为我的_Layout创建特殊的控制器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆