如何使用asp.net mvc中的用户控件显示“岛”数据的? [英] How do you use usercontrols in asp.net mvc that display an "island" of data?

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

问题描述

我想知道如何在asp.net mvc中使用usercontrols。我知道如何向视图添加用户控件,以及如何传递数据到它。我没有能够找出是如何做到这一点,而不必检索和传递每个控制器中的数据?

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?

我来自网络表单,我可以写一个用户控件,它有自己的数据和显示的数据独立于其使用的任何页面的其余部分。

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.

推荐答案

是多种方式做到的。

基本方法是


  • 填充BaseController中视图的数据(OnActionExecuting event)

  • 编写自定义操作过滤器

  • 编写应用程序控制器
  • Populate the data for the view in the BaseController (OnActionExecuting event)
  • Writing a custom action filter
  • Writing an Application Controller (the eg. is in the below links).

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>

我发现以下网址有用。

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

< a href =http://www.aaronlerch.com/blog/2008/01/26/displaying-foo-on-every-page-of-an-aspnet-mvc-application/ =nofollow noreferrer> 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-using-usercontrols-usefully/

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

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