1

I have created a controller with following action. Passing list of categories using viewdata as below

 public ActionResult Home()
        {
            ViewData["categories"] = db.Categories.ToList();
            return View();
        }

I want to render the list of categories using partial view. so partial view is created with following markup

@using test.Models

@{
    ViewBag.Title = "mnuCategories";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Browse Categories</h2>

@foreach(Category c in (IEnumerable<Category>)ViewData["categories"])
{
    @c.CategoryName
}

i am rendering this partial view on home page using

@Html.Partial("mnuCategories")

but when i am running the application it given me "stackoverflow excception"

3
  • Post the full exception message you're getting. Also, your partial view does not need to include the Layout again (or even set the Title again). The calling view will take care of that. Try removing Layout = "~/Views/Shared/_Layout.cshtml"; Commented Aug 12, 2015 at 14:37
  • thank you so much Brandon...removed ."Layout = "~/Views/Shared/_Layout.cshtml" and it worked.... Commented Aug 12, 2015 at 14:43
  • good to hear. I've posted the same comment as an answer. You can select it as the accepted answer by clicking on the checkmark next to it. Commented Aug 12, 2015 at 14:48

1 Answer 1

2

Your partial view does not need to include the Layout again (or even set the Title again). The calling view will take care of that.

Remove this block

@{
    ViewBag.Title = "mnuCategories";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
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.