0

I am going to be purchasing an MVC theme from WrapBootstrap https://wrapbootstrap.com/theme/inspinia-responsive-admin-theme-WB0R5L90S for a personal project.

There are alot of things that I like about it design wise, but it has brought up a question about the best way to load notifications in the header (really any sort of dynamic data that needs to load into the common Layout view).

I know there are a great number of ways to do this, but I am at the point in my development experience where I am trying to break my bad design habits (hacking things together so they "just work"), and look for elegant solutions where I can.

The things that make the most sense so far is to either:

1) Create a global ActionFilter that will filter all requests and throw a "LayoutViewModel" into the ViewBag, and just use the ViewBag in the _Layout view.

  • I'd rather do something strongly typed, but I don't see a way.
  • This is nice because it is available, does not require my controllers to inherit any functionality from a base class that might be cludgy, or even know that it is happening.

2) Load the page and onLoad, just do an Ajax Calls to the server to load any dynamic data I need.

  • This may have some disadvantages if your layout needs to change based on the data. I dislike with things snap all over the page after loading.

Is there a design pattern or MVC feature that I may be missing top accomplish this?

I may in the future I may implement something liks SignalR(or a simple timed ajax call to look for updates) to get updated data/notifications, but for now I am just looking at the initial page load.

Thank you for any ideas that you may have.

1 Answer 1

1

Most likely, what you're looking for is a child action. These are pretty much just like normal actions, except they'll return partial views and generally are decorated with [ChildActionOnly] to prevent routing to them via the URL bar.

[ChildActionOnly]
public ActionResult Notifications()
{
    var notifications = // get the notifications;
    return PartialView("_Notifications", notifications);
}

Then, in your layout, you just add the following where you want it to appear:

@Html.Action("Notifications", "Foo")

Where "Foo" is the name of the controller you put this child action in. I have a post on my blog that gives a primer on the Razor templating system that may be of use to you as well.

Sign up to request clarification or add additional context in comments.

2 Comments

This may be EXACTLY what I needed. I am surprised that I have never come across this, even on accident. Apparently my google-fu is not as strong as I figured. I will look into it and mark as Answer if it meets my needs. Many Thanks!
Couple of notes when using child actions. 1) Since they're not real actions, they can't redirect, particularly for things like a login. If you use them with Authorize, you should also use AllowAnonymous, or you'll have issues. 2) Unfortunately, they can't be async, so you'll need to use sync methods only inside.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.