Classes in razor engine template

Yes, this is completely possible. Use the @functions keyword: @functions { public class MyClass { public MyClass() { Three = new List<string>(); } public string One { get; set; } public int Two { get; set; } public List<string> Three { get; set; } } }

Hourly, Daily, Monthly Helper+Model methods

You can create daily,monthly and yearly data by using the same logic. You just have to replace Hour with Day,Month or Year. List<dynamic> dailyData = new List<dynamic>(); DateTime dayIterator = StartDate.Value; while (dayIterator.Hour <= ddvm.CurrentDay) { if (ddvm.DailyPaymentTotal != null && ddvm.DailyPaymentTotal.Count() > 0 ) { DailyPaymentTotalModel day = ddvm.DailyPaymentTotal.FirstOrDefault(hpt => hpt.DateTimeStamp == dayIterator); if … Read more

Javascript in a View Component

What I decided to do is write a ScriptTagHelper that provides an attribute of “OnContentLoaded”. If true, then I wrap the inner contents of the script tag with a Javascript function to execute once the document is ready. This avoids the problem with the jQuery library having not loaded yet when the ViewComponent’s script fires. … Read more

Nested TagBuilder -as TagBuilderTree-

You can build the child elements in separate TagBuilders and put their generated HTML in the parent TagBuilder. Here’s an example: A <select> with some <option>s (example de-fatted for terseness) TagBuilder select = new TagBuilder(“select”); foreach (var language in languages) // never ye mind about languages { TagBuilder option = new TagBuilder(“option”); option.MergeAttribute(“value”, language.ID.ToString()); if … Read more

The tag helper ‘input’ must not have C# in the element’s attribute declaration area

If you don’t need to use the TagHelper, you can use <!elementName> to disable it for a specific element: <!textarea class=”testClass” asp-for=”testId” @readonlyAttribute>@Model.Id</!textarea> See @glenn223’s answer for a more structural solution. I made an improved version of his solution, by adding support for anonymous objects: using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Razor.TagHelpers; namespace {YourBaseNameSpace}.Helpers.TagHelpers { [HtmlTargetElement(Attributes = “custom-attributes”)] … Read more