-
Notifications
You must be signed in to change notification settings - Fork 0
26 Add Badges using Partial Views
M. Fares edited this page Apr 6, 2018
·
1 revision
Badges can be added using partial views or JQuery/Ajax. In this procedure, we will use partial views.
-
Create an action that returns the count of the items
Example: /Controllers/DepartmentController.cs
public ActionResult GetCountFacultiesPartial(int id)
{
// Modify the condition inside the Count() to suite your needs
int count = db.Faculties.Count(p => p.DepartmentId == id);
return PartialView(count);
}-
Create the partial view that holds the count
Example: /Views/Department/GetCountFacultiesPartial.cshtml
@model int
<span class="badge">@Model</span>- Include a link to the partial view in the parent/main view. The link can also be included in the _layout.cshtml
@Html.Action("GetCountFacultiesPartial", "Department", new { id = item.Id })-
Test the results
Example: Go to /Department/Index
Note: In Bootstrap 3, the badges do not have colors. You may use CSS and Background-color to change the color of a badge.