您可以让action方法返回徽章的HTML,其中还包括数据(警告号)。
public class WarningController : Controller
{
public ActionResult Badge()
{
int contWarning = 10; // temp hard coded value for demo;
// Replace the hard coded value
// with your existing code to get the data from database
return PartialView("Badge",contWarning);
}
}
现在在你的
Badge.cshtml
,它被打印到
int
键入,呈现所需的HTML。
@model int
<span class="badge">
@Model
</span>
_Layout.cshtml
),使用
Html.Action
方法。
@Html.Action("Badge","Warning")
PartialView
而不是
View
方法。如果您的Badge action方法返回的视图具有相同的布局文件,则将导致无限循环,并且您将获得StackOverflow异常。