2

I am trying to seperate the menu from _Layout.cshtml but I am having difficulties.

My files are located like below.

Views/Home/Index
Views/Shared/_Layout
Views/Shared/_Menu

In _Layout.cshtml file, I have the code below...

@Html.Partial("_Menu")

Menu action is located in HomeController, and it looks like below

public ActionResult Menu()
{
    MenuModel menu = new MenuModel();
    return PartialView("_Menu", menu);
}

_Menu has the code below as first line

@model DomainModel.MenuModel

When I run the project on VS, everything looks perfect but I doesnt call Menu() action in HomeController. It somehow finds _Menu and displays it perfectly. But I dont understand why it doesnt call Menu() action?

2 Answers 2

3

@Html.Partial("_Menu") will just render HTML view, nothing to do with controller. If you want to call a controller use @{ Html.RenderAction("Menu", "Home"); }

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

6 Comments

Hi, It gives an error "Cannot implicitly convert type void to object".
@ayilmaz just format use this @{ Html.RenderAction("Menu", "Home"); }
well, it gives this error "A public action method '_Menu' was not found on controller 'HomeController'"
@ayilmaz @{ Html.RenderAction("Menu", "Home"); }
Perfect, thanks a lot. so @{ Html.RenderAction("Menu", "Home"); } calls the action. And action return _Menu, I guess
|
0

Actions are cally by request. Therefore you have to send a request e.g. via Ajax to YourController/YourAction or type the url in the browser.

@Html.Partial() simply renders the view with the given data.

To render a view by calling a action please refer to Ahmends answer, https://stackoverflow.com/a/40589584/3936440.

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.