'ASP.NET MVC Application: I was wondering if you can retrieve the current user in a ViewComponent?
This is the class I am currently working in, and I would to inject the current user here, is this possible? As you can see below, I tried using a filter, but then my chart wouldn't show at all.
using Microsoft.AspNetCore.Authorization;
using projecten2.Models.Domain;
using Microsoft.AspNetCore.Identity;
namespace projecten2.ViewComponents
{
[ViewComponent(Name = "chartjs")]
public class ChartJsViewComponent : ViewComponent
{
[ServiceFilter(typeof(KlantFilter))]
[Authorize]
public IViewComponentResult Invoke(Klant klant)
{
// Ref: https://www.chartjs.org/docs/latest/
string[] dataLabels2 = new string[6];
int[] data = { 5, 8, 4, 12, 10, 5 };
DateTime date = DateTime.Today;
string.Format("dd mm `yy");
DateTime date2 = new DateTime();
for(int i = 5; i >= 0; i--)
{
date2 = date.AddDays(-7);
dataLabels2[i] = string.Format("{0:dd/MM/yy}", date2) + "-" + string.Format("
{0:dd/MM/yy}", date);
foreach(Contract c in klant.Contracten)
foreach(Ticket t in c.Tickets)
{
if (t.AanmaakDatum > date2)
data[i]+=1;
};
date = date2.AddDays(-1);
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
