2

The problem i have is that when i use @Html.RenderAction or @Html.Action in the Layout and call Action method from Controller page is not displaying at all. But when i use static attributes in layout all works fine.

Layout:

  <li>
   <a href="#"><i class="fa fa-fw"></i> Category<span class="fa arrow">  </span></a>
           <ul class="nav nav-second-level">
                   @{
                     Html.RenderAction("Category","Home");
                    }
           </ul>
   </li>

Action Method:

public ActionResult Category()
    {
        int memberId = WebSecurity.GetUserId(User.Identity.Name);

        return PartialView("_Categories", _db.Query<Category>().ToList().Where(u => u.UserId == memberId));
    }

PartialView:

@model IEnumerable<PasswordCloudApp.Models.Category>

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

@foreach (var item in Model)
{
<li>
    @Html.ActionLink(item.Name, "Index", "Entry", new { id = item.Id }, null)
</li>
}

I've tried just with regular view, then with partial view, none of it works. Any suggestion?

2 Answers 2

0

Just an Idea, Not sure about it

You should remove this line from your partialview @{ Layout = "~/Views/Shared/_Layout.cshtml"; } Then give a try. Hope this helps

Sign up to request clarification or add additional context in comments.

6 Comments

Not working, i've put it there to try with it. I've now copied the layout in the Index View of Home Controller, and it's working, but that's not a solution, the idea is to work from the layout...
@PeterZ Please Debug your code line by line. There must be a minor mistake somewhere. Coz the code looks right
I am right now. I am trying now with Html.RenderPartial("_Categories") insted if RenderAction and it gives me Object reference not set to an instance of an object error on @foreach (var item in Model). So i suppose that the error comes from Action Method, but i cannot resolve it.
When u use RenderPartial You have to pass the model to it. So Pass the Model Such as @Html.RenderPartail("_Categories", ViewBag.List as List<Category>)
I've found a solution. Delete the current Layout, then created new one, copied everything from the old, and everything is working fine. Why the old layout is causing problems, don't know. Thanks for your help.
|
0

As per my understanding please return the full path of view for example ~/Views/Shared/_Categories.cshtml" instead of _Categories. If you want layout then use same code else use as

@{ Layout = null; }

Remove Layout and use as below:

@Html.Action("Category","Home")

4 Comments

Not working. I've put the body part from Layout view in the Index view, and the Layout leave it just with RenderBody(), that way it works, but now the layout is empty. Any toughts why in Index is working and on Layout is not?
No error, nothing, page is just not displaying. Just loading and at the end no result, none. Just like when you searchng page that don't exists. This webpage is not available
Can you see db is returning data or not. If not then it should display none.
db is returning data. And it works very well in Index when i call Html.RenderAction("Category", "Home"), but in Layout when i use RenderAction, don't.

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.