I'm currently generating breadcrumbs on an object's Details page by calling a GetBreadcrumbs() method in the object's controller - in this method, the object's parent/grandparent are used to generate an unordered list. What's the best way to pull the HTML out of the controller to follow the Separation of Concerns paradigm? Should a partial view be used here?
2 Answers
Typical example of partial view is Breadcrum itself. For example, in your controller you can have
//
//GET: News/Article/x
public ActionResult Article(int id)
{
//get parentid of article
ViewBag.id = id;
ViewBag.parentid;
return View();
}
So, your partial view will be as below:
@{
ViewBag.Title = "Article";
}
<h2>Viewing Article @ViewBag.parentid >> @ViewBag.id</h2>
1 Comment
user2062383
Thank you. What if an Article has a parent Article, which may or may not have a parent Article, etc etc? Where would this logic of looping through the parents belong?
You could use partial views or display templates. Your controller should only build the model that will be passed to the view and then inside the view you could use a display template that will build the desired output based on the model.
1 Comment
user2062383
Should this model (that I'm passing to the view) also include all of the parent objects that are required to build the breadcrumbs?