Difference between revisions of "Using partial with controller"

From Logic Wiki
Jump to: navigation, search
(Created page with "Category:MVC Category:CSharp Category:Razor == Controller and Action == <pre class="brush:csharp;"> public class MyController { public ActionResult Index() {...")
(No difference)

Revision as of 09:28, 11 May 2017


Controller and Action

public class MyController {
  public ActionResult Index() {
    return View();
  }
  
  [ChildActionOnly]
  public ActionResult Menu() {
    var menu = GetMenuFromSomewhere();
      return PartialView(menu);
  }
}

Partial View

The Menu action grabs the Menu model and returns a partial view with just the menu.


<%@ Control Inherits="System.Web.Mvc.ViewUserControl<Menu>" %>
<ul>
<% foreach(var item in Model.MenuItem) { %>
  <li><%= item %></li>
<% } %>
</ul>

View

<%@ Page %>
<html>
<head><title></title></head>
<body>
  <%= Html.Action("Menu") %>
  <h1>Welcome to the Index View</h1>
</body>
</html>