1

I wanted use MVC and renderpartial to generate a menu but but could not get it to work, and from what I read it seemed maybe RenderAction would be more suitable. Still I have not gotten it to work.

What I intended to do was create a controller that selects certain articles from a database that will act as categories (this is put into HomeController):

public ActionResult MenuController()
    {
        var movies = from m in db.Art
                     where m.ArtikelNr.StartsWith("Webcat")
                     select m;
        return View(movies);
    }

And then send that information to a view:

@model IEnumerable<xxxx.Models.Art>
@{
Layout = null;
}

<ul>
@foreach (var item in Model)
{
<li>@Html.DisplayFor(modelItem => item.Benämning_10)</li>
}

This works when I just run it as a normal controller and view, it returns a list of what I want. But if I want to call it from _layout.cshtml (because this menu should appear on every page) like this:

<div id="sidebar">@Html.RenderAction(MenuController)</div>

Then it generates the following error:

CS0103: The name 'MenuController' does not exist in the current context

What is the proper way of calling an action/view/whatever from the _layout.cshtml file?

Dennis
  • 373
  • 1
  • 6
  • 21

4 Answers4

1

You should call

@Html.RenderAction("_MenuController")

and be sure that you have a working rule in your Global.asax

As suggested in another answer would be better to use

return PartialView();

I also suggest you to use the ChildActionOnlyAttribute to be sure that this action will never be called as a standard action.

So something like that:

[ChildActionOnly]
public PartialViewResult _MenuController()
{
    var movies = from m in db.Art
                 where m.ArtikelNr.StartsWith("Webcat")
                 select m;
    return PartialView(movies);
}
Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
Iridio
  • 9,213
  • 4
  • 49
  • 71
  • Thanks for these tips, I have implemented them (still getting error CS1502), but what do you mean with having a working rule in global.asax? Rule for what? – Dennis Feb 08 '12 at 11:38
  • 1
    I would just like to say I was able to get it working now, for some reason it would not work unless it was typed like this, can anyone explain to me why this works? `` – Dennis Feb 08 '12 at 12:02
  • For the global.asax I was suggesting to be sure to che ck the rule in case you have modified the standard rule (/{controller}/{action}/{id}) or it would not find the action. For the second tips you are excuting a function. look here for more info http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx – Iridio Feb 08 '12 at 13:42
1
@{Html.RenderAction("MenuController");} 

or

@Html.Action("MenuController")
Yorgo
  • 2,668
  • 1
  • 16
  • 24
0

Simply

@Html.RenderAction("MenuController")

You've forgotten quotes around your string parameter

Oybek
  • 7,016
  • 5
  • 29
  • 49
  • Thanks, that's true, a bit embarrasing. But still that now generates another error: CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments – Dennis Feb 08 '12 at 11:25
0
<div id="sidebar">@Html.RenderAction("_MenuController")</div>

Quotes around your action name :) It might also be good practice to return a partial view:

return PartialView(movies);
Chris
  • 3,191
  • 4
  • 22
  • 37