如何使_Layout.cshtml中的导航栏中的徽章获得价值 [英] How to make Badge in navbar located in _Layout.cshtml receive value

查看:166
本文介绍了如何使_Layout.cshtml中的导航栏中的徽章获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ControllerWarnings的所有应用程序信息导航中,如何制作_Layout.cshtml导航栏中的徽标?

How to make the Badge that is in the navbar that is in the _Layout.cshtml capture in all the navigation of my application information of my ControllerWarnings?

在我的ControllerWarning中,我有一个函数,当重新请求时,它将在Json中返回一个数值.

In my ControllerWarning I have a function that when requesited returns a numeric value in Json.

namespace VS.Controllers
{
    public class ControllerWarning : Controller
    {
        private VSContext db = new VSContext();

        public JsonResult GetWarning(string user)
        {
            DateTime dt = DateTime.Now.Date;
            int contWarning = 0;

            var listaAvisos = db.Warnings.Where(a => a.User== user).ToList();
            var l = new List<Aviso>();

            foreach (var item in listaAvisos)
            {
                var res = item.Data - dt;
                item.QtdDias = res.Days;

                if (res.Days <= 5 && item.Enviado != true)
                {
                    contWarning++;
                }
            }

            return Json(contWarning);
        }
      }

      public ActionResult Index(){...}
      public ActionResult Details(int? id){...}
      public ActionResult Details(Warning warning){...}
 }

位于_Layout.cshtml中的导航栏具有一个徽章,应该收集从WarningController返回的GetWarning(字符串用户)返回

<li>
    @Html.ActionLink("Warning", "Index", "Warnings")
         <span class="badge">

                 @*HERE VALUE RECEIVED GETWARNING*@
                 GetWarning(string user)

         </span>
</li>

推荐答案

您可以让您的操作方法返回徽章的HTML,其中还包括数据(警告编号).

You can have your action method returns the HTML for your badge, which includes the data(warning number) as well.

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.

Now in your Badge.cshtml, which is storngly typed to int type, render the HTML you want.

@model int
<span class="badge">
    @Model
</span>

现在在您的布局(_Layout.cshtml)中,使用Html.Action方法调用此渲染此Badge动作方法的输出.

Now in your layout(_Layout.cshtml), call this render the output of this Badge action method using the Html.Action method.

 @Html.Action("Badge","Warning")

确保使用PartialView而不是View方法返回部分视图(不具有其自己的布局).如果您的Badge操作方法返回的视图具有相同的布局文件,则将导致无限循环,并且您将获得StackOverflow异常.

Make sure you are returning a partial view (which does not have it's own layout) using PartialView instead of View method. If your Badge action method is returning a view which has the same layout file, that will cause an infinite loop and you will get the StackOverflow exception.

这篇关于如何使_Layout.cshtml中的导航栏中的徽章获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆