Difference between revisions of "Using partial with controller"
From Logic Wiki
(Created page with "Category:MVC Category:CSharp Category:Razor == Controller and Action == <pre class="brush:csharp;"> public class MyController { public ActionResult Index() {...") |
|||
| Line 10: | Line 10: | ||
} | } | ||
| − | + | [ChildActionOnly] | |
| − | + | public ActionResult _ApplicationStateMessage() | |
| − | + | { | |
| − | + | var model = State.Get(MyApp.UserId); | |
| − | + | ||
| + | return PartialView(model); | ||
| + | } | ||
} | } | ||
</pre> | </pre> | ||
| Line 21: | Line 23: | ||
The Menu action grabs the Menu model and returns a partial view with just the menu. | The Menu action grabs the Menu model and returns a partial view with just the menu. | ||
<pre class="brush:csharp;"> | <pre class="brush:csharp;"> | ||
| − | + | @model PlatformModels.ApplicationState | |
| − | + | <div> | |
| − | < | + | @foreach (var msg in Model.ApplicationStateMessages) |
| − | + | { | |
| − | + | <div> | |
| − | + | @($"{msg.Title}") | |
| − | </ | + | </div> |
| + | <div> | ||
| + | @($"{msg.Content}") | ||
| + | </div> | ||
| + | } | ||
| + | </div> | ||
</pre> | </pre> | ||
== View == | == View == | ||
<pre class="brush:csharp;"> | <pre class="brush:csharp;"> | ||
| − | + | @Html.Action("_ApplicationStateMessage") | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
</pre> | </pre> | ||
Revision as of 09:29, 11 May 2017
Controller and Action
public class MyController {
public ActionResult Index() {
return View();
}
[ChildActionOnly]
public ActionResult _ApplicationStateMessage()
{
var model = State.Get(MyApp.UserId);
return PartialView(model);
}
}
Partial View
The Menu action grabs the Menu model and returns a partial view with just the menu.
@model PlatformModels.ApplicationState
<div>
@foreach (var msg in Model.ApplicationStateMessages)
{
<div>
@($"{msg.Title}")
</div>
<div>
@($"{msg.Content}")
</div>
}
</div>
View
@Html.Action("_ApplicationStateMessage")