2

In a nutshell, I would like to simplify some of my MVC pages by creating a static class to get the data from a data store of some sort then return one of multiple views based on some internal flags.

internal static class StaticPageContent
{
    internal static ViewModels.Display GetPage(string Map = null, int? ID = null) {...}
    internal static ActionResult ReturnView(ViewModels.CMS.Display vm) {...}
}

Then, in any of my controller actions I could call it something like:

public ActionResult ActionName()
{
    var vm = StaticPageContent.GetPage("/Home");
    return StaticPageContent.ReturnView(vm);
}

Where the ReturnView() method would return one of multiple (shared) views:

internal static ActionResult ReturnView(ViewModels.CMS.Display vm)
{
    if (vm.useLoremIpsum)
        return View("LoremIpsum", vm);
    else
    {
        if (vm.canEdit)
            return View("ViewReadWrite", vm);
        else
            return View("ViewReadOnly", vm);
    }
}

What is the right way of returning a View(...) object from a non-Controller class? The above will not compile as the View(...) is part of the Controller class.


NOTE: I believe I got something like this working but the demands on my time sent me in another direction. I am hoping to be able to open this project back up to see what I did and post it here. Stay tuned!

Keith Barrows
  • 24,802
  • 26
  • 88
  • 134
  • I've created something that will hopefully point you in the right direction: http://stackoverflow.com/questions/3808076/using-the-razor-view-engine-in-a-different-way/3815216#3815216 – Buildstarted Oct 04 '11 at 04:43

2 Answers2

1

Simply put, the right way is to not do it in a static class. (IMO, of course). Especially if you're getting "data from a data store" - this should not be static.

In my opinion, create a protected method in a base controller, or use child actions and render them out using Html.Action.

halfer
  • 19,824
  • 17
  • 99
  • 186
RPM1984
  • 72,246
  • 58
  • 225
  • 350
  • It's been awhile since I visited this question. Just a curiosity question - why do you think static class is bad in this situation? I do understand it is a singleton and could become a bottle neck. Anything outside of that? My intention with this is to basically populate an Editor or a Read Only page with data from a data source. The view definition would be the same across the site (across various routes). I'm just basically looking for an easy way to write a single generic view usable across the site. :) – Keith Barrows Apr 06 '12 at 16:00
  • @KeithBarrows - yes, singleton is one reason. Another is loose coupling - static classes cannot be implemented behind interfaces and thus cannot be easily swapped out or unit tested. – RPM1984 Apr 08 '12 at 09:00
  • I created a base controller. The "static view" is basically a filler until I get around to writing the real data binding magic for that page. In other words, I can create a website with a lot of "Ipsum Lorem" pages then start building out the logic for each page so my client(s) can see the progress being made and still have a feel for the final site with dummy data. – Keith Barrows Jan 29 '13 at 22:45
0

Just instantiate one, though I don't think I would recommend this.

http://msdn.microsoft.com/en-us/library/system.web.mvc.viewresult.aspx

var result = new ViewResult(){
    ViewName = "ViewReadWrite"
}
result.ViewData.Model = vm; // ViewData may need to be instantiated first.

return result;
Daryl Teo
  • 5,394
  • 1
  • 31
  • 37