views:

42

answers:

1

I’m building an application that monitors other systems. Now I want to implement partial view, a User Control called “status”. This control shall display status information about the application.Like:

user logged in,

How many systems online,

Latest activity.

This partial view shall be rendered in nearly all other views. How shall I pass this information to the view?

I don’t want to write

Wiewdata[“SystemsOnline”] = Helpers.CountSystemsOnline() 
Wiewdata[“SystemLatestActivity”] = ………………

in all my actions.

Can I write something like Html.RenderPartial(../Shared/Status) that fist go to an action that adds the viewdata?

Or shall i access the information directly in the view trough the hepler?

I noticed that the defult LogOnUserControl view use Page.User.Identity.Name to directly access that data.

When is it ok to not pass data throug viewdata in the controller?

+2  A: 

You have to use the ViewData for that purpose. I don not recommend to to invent tricks instead.

But you do not need to manually pass the same data from every action. Just do it in your base controller:

protected override void OnActionExecuted(ActionExecutedContext context) {
    base.OnActionExecuted(context);
    if (context.Result is ViewResult) {
        // Pass the systems online to all views
        ViewData[“SystemsOnline”] = Helpers.CountSystemsOnline();
    }
}

Only couple of lines of code and you have it everywhere.

Dmytrii Nagirniak