0

I have to following code and there's a problem and I can't figure out what's the problem. Well, I have two ideas about the place where the problem may appear but I can't find a solution.

The problem is that my layout is rendered twice.

Layout.cshtml

<div id="container">
    <div id="left_side"> @RenderPage("left_side.cshtml") </div>   
    <div id="center_side"> @RenderBody() </div>
    <div id="right_side"> @RenderPage("right_side.cshtml") </div>
</div>

left_side.cshtml

@if (ViewBag.LeftColumnVisible == true)
{
    @Html.Action("GetCategories", "Products");
}

GetCategories method from Products controller

public ActionResult GetCategories()
{
   List<Categories> categories = db.Categories.ToList();
   ...

   return View();
}

GetCategories.cshtml

@foreach (System.Collections.DictionaryEntry de in ViewBag.LeftColumnContent)
{
    <div> @((Ads.Models.Categories)(de.Key)).Name; </div>
}

It enters the get categories and renders the content.

The problem for rendering twice may be at this line @Html.Action("GetCategories", "Products"); or when it calls View(). If I comment the line, my layout will be rendered only once.

2
  • 1
    Is GetCategories supposed to be a partial view? Commented Feb 4, 2014 at 12:24
  • Yes, it's a kind of helper. I render the categories there and then I get the result in the left side which will be passed to the layout. Commented Feb 4, 2014 at 12:39

1 Answer 1

1

I'd say there are a couple of issues with your code. First of all, if left_side.cshtml/right_side.cshtml are partial views then you want to be using

@Html.RenderPartial("view")

to render them and not @RenderPage - although @RenderPage will work it's better from a readability point of view to understand exactly what the type of view it is you are working with.

Secondly, if your GetCategories view is a partial view you want to be returning a PartialView and not a View i.e.

public ActionResult GetCategories()
{
    ...
    return PartialView();
}
Sign up to request clarification or add additional context in comments.

Comments

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.