1

I have a user control which shows list of latest announcements. This user control would be used in almost 90% of my pages. Now my concern is how to pass data to this user control for latest announcements.

My first approach is to make a base controller and in Initialise method I pass data to user control via ViewBag/ViewData. All my other controllers derive from this base controller. This looks nice but my concern is that it may become an overkill for some simple solution existing already out there. Also I would need to make sure that no controller ever fiddles with my Viewdata/Viewbag data meant for my usercontrol.

Please let me know if this is correct way of proceeding ahead or there exists some better solution.

Thanks

RPM1984
  • 72,246
  • 58
  • 225
  • 350
Naveen
  • 1,067
  • 2
  • 14
  • 36

4 Answers4

0

Is this the kind of thing you're after? How to pass data from view to UserControl in ASP.NET MVC?

Community
  • 1
  • 1
Ian Oxley
  • 10,916
  • 6
  • 42
  • 49
  • Nopes, my control expects a strongly typed list. I am unsure how to pass data to this control. It has to be some central place and I felt a base controller wud be a good option. Wat say ? – Naveen Dec 20 '10 at 11:27
0

Assuming you have a "user control" (you should try to refer to them as partial view's in MVC) that looks like this:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Announcement>>" %>

This means your partial view expects a list of Announcement objects.

Now, the question is - where are you rendering this partial view?

You could be doing it from a master page, you could be doing it from a view, or you could be doing it from another partial view.

Either way, the code to render the partial needs to look like this:

<% Html.RenderPartial("LatestAnnouncements", announcements) %>

But - where do you get the announcements from.

Assuming you have a Repository/DAL/helper method to get the latest announcements - i think you should have the ViewModel's you require inheriting from a base ViewModel:

public class AnnouncementViewModelBase
{
   protected IEnumerable<Announcement> GetAnnouncements()
   {
       // call DAL
   }
} 

Then any master/view/partial that needs to render the latest announcements partial should be bound to a ViewModel which inherits from that base view model.

In the cases where the master/view/partial is not strongly-typed (e.g dynamic view), you can stick it in the ViewData. But if you have organized your view's correctly this shouldn't be required.

RPM1984
  • 72,246
  • 58
  • 225
  • 350
  • going ahead with this approach, I want to pass data from master page to user control, but there is no code behind for master page. How to do go ahead with it then ? – Naveen Dec 20 '10 at 11:56
  • Yup - that makes it tough. YOu have two options - 1) put the latest announcements in ViewData. 2) Use RenderAction to call a action method which grabs the latest announcements and renders a `PartialViewResult`. Option 2 would probably be easier. – RPM1984 Dec 20 '10 at 20:52
  • RenderAction sounds good.. This would probably mean i create a list of actions for all such user controls and use it. You didn't tell me how is idea for Base Controller sounds ? – Naveen Dec 21 '10 at 08:14
  • 1
    No - you just need one action, which can be called from anywhere. The reason i don't like the Base Controller is that Controllers should be created usually per functional area. "Latest Announcements" is a small function that should belong only to one controller, not a whole area - so i don't see how multiple controllers would require this behaviour. – RPM1984 Dec 21 '10 at 08:30
  • just one thing.. renderaction is useless out of parentcontroller. If I want to invoke an action out of a controller how to do it ? – Naveen Dec 21 '10 at 12:14
0

You should use RenderAction in this kind of scenario, so that you do not have bother to pass the required data in each action method of your controllers.

Kazi Manzur Rashid
  • 1,544
  • 13
  • 14
0

I think the best way would be to use @Html.Action. This would allow me to call my actions dedicated to my usercontrols data and I can call it from anywhere.

Naveen
  • 1,067
  • 2
  • 14
  • 36