0

I have a master layout for a conference registration system, what I'm trying to do is to display the number of registered users in the master layout.

What is the best way to do that ?

I can call a static method to get this count, but from my knowledge, this is a violation of the MVC standards, because interaction between a page and the server should be only through Controller/Action method.

So what is the best way to do this ?

tereško
  • 58,060
  • 25
  • 98
  • 150
sameh.q
  • 1,691
  • 2
  • 23
  • 48

2 Answers2

1

When I need to display data in the layout page, particularly if it's a small amount of data, I usually put that data into ViewBag/ViewData. Otherwise you need a base class model that every developer has to remember to inherit from to carry the data into the view. The ViewBag/ViewData can then be populated by an OnResultExecuting filter on the base controller class.

Craig W.
  • 17,838
  • 6
  • 49
  • 82
  • I think this solution requires calling a method on each action call for the views that this value should appear on, right ? – sameh.q Apr 20 '14 at 15:33
  • No, it requires setting up an `OnResultExecuting` filter on your base controller, like I said in the original post. – Craig W. Apr 20 '14 at 16:52
  • you are right, since I'm already using a base controller class for my controllers this will do it, thanks – sameh.q Apr 22 '14 at 07:03
0

You need to have main Model/ViewModel for all of your pages or use two or multiple Models/ViewModel in targeted pages. In this way you pass all required data to view via controller methods perhaps by calling a static method in methods. see the following articles to employ the above techniques:

  1. Two models in one view in ASP MVC 3
  2. Multiple models in a view

Another way is storing related data in ViewBag or ViewData Dynamic types in controller methods each time needed and then prepare and use them in View. Note that using them should be avoided in favor of using strongly typed ViewModels.

cheers.

Community
  • 1
  • 1
Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70