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() {...") |
|||
| (One intermediate revision by the same user not shown) | |||
| Line 10: | Line 10: | ||
} | } | ||
| − | + | [ChildActionOnly] | |
| − | + | public ActionResult _ApplicationStateMessage() | |
| − | + | { | |
| − | + | var model = State.Get(MyApp.UserId); | |
| − | + | ||
| + | return PartialView(model); | ||
| + | } | ||
} | } | ||
</pre> | </pre> | ||
== Partial View == | == Partial View == | ||
| − | |||
<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> | ||
Latest revision as of 09:30, 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
@model PlatformModels.ApplicationState
<div>
@foreach (var msg in Model.ApplicationStateMessages)
{
<div>
@($"{msg.Title}")
</div>
<div>
@($"{msg.Content}")
</div>
}
</div>
View
@Html.Action("_ApplicationStateMessage")