views:

165

answers:

2

If I create a PartialView for a box that holds a header, image and content what is the best way to store the content without using a database?

Example: TurboTax

I doubt the content for the boxes in the sidebar are stored in a database but to make reusable code it would be beneficial to create the structure in a PartialView and populate the content areas. I can create a PartialView and pass a Model from the parent Controller to the PartialView but then I would be stuck copying and pasting that same content if I wanted to use the same box on another page.

+1  A: 

Passing data to partial view that is used in many places can be done in many ways:

Create base model class for all your models. In base class define PartialModel property which will be holding model for partial view (there may be many of them if use have many partial views). Now you can populate the PartialModel property in controller action, but to make code more reusable you can create your own Action Filter which will insert the partial view data just after the action method is executed (but before the model is passed to the view)

public class PartialViewModelAttribute : ActionFilterAttribute
{
  public override void OnActionExecuted(ActionExecutedContext filterContext)
  {
     BaseViewModel model;

     if (filterContext.Controller.ViewData.Model == null)
     {
          model = new BaseViewModel();
          filterContext.Controller.ViewData.Model = model;
     }
     else
     {
        model = filterContext.Controller.ViewData.Model as BaseViewModel;
     }

     model.PartialModel = new PartialModel(...)   // Partial model initialization
     base.OnActionExecuted(filterContext);
  }
}

Then you can use it like:

[PartialViewModel]
public ActionResult Index()
{
  //...
}

Another option: you can create BaseController class for all your controllers and create PartialModel on base controller initialization. Then PartialModel can be stored in ViewData[] dictionary. Because using ViewData dictionary in views is bad, create extension method on HtmlHelper like:

public static PartialModel GetPartialModel(this HtmlHelper helper)
{
   return helper.viewContext.ViewData["PartialModel"] as PartialModel
}

So you could obtaint the model this way:

<% Html.RenderPartial("MyPartial", Html.GetPartialModel()); %>
PanJanek
+1  A: 

For fixed content you might want to think about using XML+XSLT or even HTML snippets in the file system and simply rendering them. An HtmlHelper method might make more sense for this than a partial view - Html.RenderXml() or Html.Include(). The only real difference between these and partial views is that the view engine isn't invoked since there aren't any substitutions. I do this sort of thing with my privacy policy and terms and conditions. I'd certainly consider keeping these cached.

If these really are templates and you are just substituting content, then I think the partial view works well and I would consider putting the data in a database, again, maybe using caching if I found that performance suffered. You could use this in combination with the former -- say keep your images/xml in the file system and a pointer to them in the database so you know which ones to pick in the partial.

tvanfosson