Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 450
dtc Avatar asked Nov 29 '08 01:11

dtc


People also ask

How can I ViewData in ASP NET MVC?

You can now access ViewData["students"] in the view, as shown below. Above, we retrieve the value using ViewData["students"] and typecast it to an appropriate data type. You can also add KeyValuePair objects into the ViewData, as shown below. ViewData and ViewBag both use the same dictionary internally.

How do you pass data between a controller and a view?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.

What is mvc4?

ASP.NET MVC 4 is a framework for building scalable, standards-based web applications using well-established design patterns and the power of the ASP.NET and the . NET framework. This new, fourth version of the framework focuses on making mobile web application development easier.


2 Answers

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

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/

like image 126
rajesh pillai Avatar answered Sep 20 '22 01:09

rajesh pillai


In the MVC Futures, available on codeplex , contains the RenderAction HtmlHelper extensions. This will allow you to create a controller for the ueser control and this controller will populate the ViewData used by the user control without having to resort to a base controller as was suggested.

In the View you would do

<% Html.RenderAction("Index", "UserControlController") %>

or one of the other overloads.

This will create an instance of the controller, execute the method and render the user control view into the main view. The main view controller does not need to know anything about the user control or its model/data.

like image 31
Matthew Avatar answered Sep 20 '22 01:09

Matthew