user2062383
user2062383

Reputation: 959

Constructing HTML in Controller - how to refactor?

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?

Upvotes: 0

Views: 59

Answers (2)

Bhushan Firake
Bhushan Firake

Reputation: 9448

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>

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

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.

Upvotes: 0

Related Questions