How do you use usercontrols in asp.net mvc that display an “island” of data?

前端 未结 4 1978
时光说笑
时光说笑 2021-01-30 09:34

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 i

4条回答
  •  -上瘾入骨i
    2021-01-30 10:16

    There are multiple ways to do it.

    The basic approach is

    • 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).

    An example of OnActionExecuting will be

       [HandleError]
        public class BaseController : Controller
        {
            CourseService cs = new CourseService();
            protected override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                List 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.

    
    

    I found the following URL to be useful.

    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

    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/

提交回复
热议问题