How to make Badge in navbar located in _Layout.cshtml receive value

后端 未结 1 1093
别跟我提以往
别跟我提以往 2021-01-25 14:18

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?

In my Con

相关标签:
1条回答
  • 2021-01-25 14:44

    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);
       }
    }
    

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

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

    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")
    

    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.

    0 讨论(0)
提交回复
热议问题