tzortzik
tzortzik

Reputation: 5133

MVC Entity Framework Razor Issue

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.

Upvotes: 0

Views: 85

Answers (1)

James
James

Reputation: 82096

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();
}

Upvotes: 1

Related Questions