-1

I'm creatig an MVC project from scratch and I can't call a controller and its view from _Layout.

  • I created View XMenu in folder Views/XMenu/

  • It has its own controller XMenuController in folder Controllers/

enter image description here

In _Layout, I call controller using Html.RenderAction. I also tried Html.Action but with no success.

<div>
      @{ @Html.RenderAction("MenuAction", "XMenu") };           
</div>

Controller XMenu

 public class XMenuController : Controller
    {
        //
        // GET: /XMenu/

        public ActionResult Index()
        {

            return View();
        }

        [ChildActionOnly]
        public ActionResult MenuAction()
        {        
        return PartialView();

        }

    }

View XMenu:

My Menu

I haven't put any parameters yet, just trying to call a controller and its view from _Layout, but get error:

Compiler Error Message: CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments

  • Is it possible to call a controller and its view from _Layout?
  • How should I call it?

EDIT:

I tried @Html.Action("MenuAction", "XMenu") };

enter image description here

I tried renaming view to MenuAction.cshtml.

enter image description here

I tried @{ Html.RenderAction("MenuAction", "XMenu"); }; enter image description here

I renamed my Controller and my View.

enter image description here

7
  • Why are you not trying @Html.Partial()? It loads view with appropriate action. Commented Apr 18, 2016 at 15:49
  • Yes, I tried @Html.Partial(), but it loads only partial view, without its controller. And later I will need its controller's methods for passing parameters. Commented Apr 18, 2016 at 15:55
  • maby @{ Html.RenderAction("MenuAction", "XMenu") };? I mean @ char problem? Commented Apr 18, 2016 at 15:58
  • Rename your partial view as MenuAction.cshtml and try. Or @Html.Action("MenuAction", "XMenu") }; Commented Apr 18, 2016 at 15:59
  • Have you tried @{ Html.RenderAction("MenuAction", "XMenu") }, without the second @ and semicolon? Commented Apr 18, 2016 at 16:11

1 Answer 1

0

I tried to compile it. And belive your problem is with your method signature. It should be:

@{ Html.RenderAction("MenuAction", "XMenu"); };
Sign up to request clarification or add additional context in comments.

1 Comment

@Delmonte you do something wrong, i create new project and all works fine after my change

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.