Showing posts with label ASP.NET MVC. Show all posts
Showing posts with label ASP.NET MVC. Show all posts

Monday, October 30, 2017

The most productive of evenings

It has been an extremely productive evening, both on the code front and on the DevOps front.

Code:

I had a bug where I was clearing a div and then attempting to repopulate it.  For the life of me, I could not get it to populate any HTML after calling jQuery's empty() function.


    

    function removeAllSections() {
        var agendaId = $('#AgendaId').val();
        $.post("/Agenda/SectionDeleteAll",
            {
                "agendaId": agendaId
            },
            function () {
                var agendaDiv = $("[name='agenda-nestable']");
                agendaDiv.empty();
            }, "json");
    }

Oh, haha, silly me. The div contains an ol, and I was clearing the contents of the div, and then attempting to add list elements to an ol that did not exist. Fixed:
    

    function removeAllSections() {
        var agendaId = $('#AgendaId').val();
        $.post("/Agenda/SectionDeleteAll",
            {
                "agendaId": agendaId
            },
            function () {
                var agendaRoot = $("[name='agenda-nestable'] ol#agenda-root");
                agendaRoot.empty();
            }, "json");
    }



On the DevOps front, I was wondering why my Razor Views take so long to load, even with Azure "Always On" enabled.

Well, tonight I found this beauty:


(then click Configure link)


Boom, precompiled Razor CSHTML views stored in their own Managed Assembly.  

Things are running much faster now!!!  No more initial page load taking forever (while the .cshtml compiles).  




Thursday, September 7, 2017

HTML Helpers - ASP.NET MVC's equivalent of JSTL Custom Taglibs

I have found the equivalent of Java's JSTL Custom Taglibs: HTML Helpers.

I started moving logic out of my Razor Views and into HTML Helper classes.

Here's my first, and it's a beauty (uses recursion to build out nested lists):


    public static class CategoryExtensions
    {
        public static IHtmlString CategoryDisplay(this HtmlHelper helper, IEnumerable categories, int indentSize)
        {
            string indentString = "";
            for (int i = 0; i < indentSize; i++)
            {
                indentString += " ";
            }
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("
\n"); stringBuilder.Append(BuildList(categories, indentString)); stringBuilder.Append(indentString).Append("
"); return new HtmlString(stringBuilder.ToString()); } private static IHtmlString BuildList(IEnumerable categories, string indentString) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(indentString).Append("
    \n"); foreach (Category category in categories) { stringBuilder.Append(indentString).Append("
  1. \n"); stringBuilder.Append(indentString).Append("
    \n"); stringBuilder.Append(indentString).Append(" " + category.CategoryName + "\n"); stringBuilder.Append(indentString).Append("
    \n"); if (category.SubCategories.Any()) { indentString += " "; stringBuilder.Append(BuildList(category.SubCategories, indentString)); } stringBuilder.Append(indentString).Append("
  2. \n"); } stringBuilder.Append(indentString).Append("
\n"); return new HtmlString(stringBuilder.ToString()); } }
I'm glad to get the logic out of the Razor views (which should not have such complex logic) and into proper C# classes.