你如何使用asp.net mvc的是显示一个&QUOT用户控件;岛"数据的? [英] How do you use usercontrols in asp.net mvc that display an "island" of data?

查看:165
本文介绍了你如何使用asp.net mvc的是显示一个&QUOT用户控件;岛"数据的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何在asp.net mvc的用户控件使用。我知道如何将一个用户控件添加到视图,以及如何将数据传递到它。我一直无法弄清楚是怎么做到这一点,而无需检索,并在每个单个控制器传递数据?

I am trying to find out how to use usercontrols in asp.net mvc. I know how to add a usercontrol to a view and how to pass data to it. What I haven't been able to figure out is how do you do this without having to retrieve and pass the data in every single controller?

例如,如果我有一个显示在几个最新的职位,但不是所有的网站的网页的用户控件,我怎么写控制器,使我得到了用户控件的数据并把它传递到用户控件只从一个在该网站而不是获取以及在每个用户控制是在所用的不同的控制器的传递数据的地方?

For example, if I have a user control that displays the most recent posts on several but not all the pages in the site, how do I write the Controllers so that I get data for that usercontrol and pass it to the user control from only one place in the web site instead of getting and passing data in each of the different controllers that the user control is used in?

我不知道,如果这是有道理,还是不行。有没有办法处理要显示在多个页面的数据孤岛一个更好的或推荐的方式?

I'm not sure if this makes sense or not. Is there a better or recommended way to handle an "island" of data that you want to display on several pages?

我是从Web表单在那里我可以只写了自己的数据和从任何网页中使用它的其余部分独立显示的数据的用户控件来了。

I'm coming from web forms where I could just write a user control that got its own data and displayed data independently from the rest of whatever page it is used on.

推荐答案

有多种方法可以做到这一点。

There are multiple ways to do it.

的基本方法是


  • 填充数据在BaseController(OnActionExecuting事件)视图

  • 编写自定义操作过滤器

  • 编写应用程序控制器(如在,在下面的链接)。

OnActionExecuting的一个例子将是

An example of OnActionExecuting will be

   [HandleError]
    public class BaseController : Controller
    {
        CourseService cs = new CourseService();
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            List<Tag> tags = cs.GetTags();
            ViewData["Tags"] = tags;
        }

    }

您可以使用任何视图中的标签​​查看数据。这仅仅是用户控件的一个例子被呈现为端内容。

You can use the "tags" view data on any view. This is just an example of usercontrol being rendered as side content.

<div id="sidebar_b">
         <asp:ContentPlaceHolder ID="ContentReferenceB" runat="server" >
             <% Html.RenderPartial("Tags"); %>
         </asp:ContentPlaceHolder>
   </div>

我发现下面的网址是有用的。

I found the following URL to be useful.

<一个href=\"http://weblogs.asp.net/stephenwalther/archive/2008/08/12/asp-net-mvc-tip-31-passing-data-to-master-pages-and-user-controls.aspx\" rel=\"nofollow\">http://weblogs.asp.net/stephenwalther/archive/2008/08/12/asp-net-mvc-tip-31-passing-data-to-master-pages-and-user-controls.aspx

http://blog.matthidinger.com/2008/02/21/ASPNETMVCUserControlsStartToFinish.aspx

<一个href=\"http://www.aaronlerch.com/blog/2008/01/26/displaying-foo-on-every-page-of-an-aspnet-mvc-application/\" rel=\"nofollow\">http://www.aaronlerch.com/blog/2008/01/26/displaying-foo-on-every-page-of-an-aspnet-mvc-application/

http://blog.wekeroad.com / 2008/01/07 / ASPNET-MVC-使用,用户控件,有效/

这篇关于你如何使用asp.net mvc的是显示一个&QUOT用户控件;岛&QUOT;数据的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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