ASP.NET MVC Html Additional Extensions and Rendering of Required "Include" s

I create my own Html Helper extension as follows:

public static string DatePicker(this HtmlHelper helper, string name, string value)
{
        return string.Format(@"<script type='text/javascript'>
$(document).ready(function(){{
    $('#{0}').datepicker({{ 
        changeMonth: true, 
        changeYear:true, 
        dateFormat: 'd-M-yy', 
        firstDay: 1, showButtonPanel: 
        true, 
        showWeek: true 
    }});
}});
</script>
<input type='text' name='{0}' id='{0}' value='{1}'>", name, value);
}

The problem is that this now requires the page to "include" the following:

<script src="/Scripts/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.ui.datepicker.min.js" type="text/javascript"></script>

And a few more items. The questions are:

  • Is there any serious processing overhead if I had to include these elements on an EACH page (for example, in Site.Master for example), thus denying the need for the HtmlHelper to organize “includes” - given that in the end about 20 includes everything various types of jQuery user interfaces are used throughout the site.

  • HtmlHelper    "",    DatePicker (    ) -    ,         , ,    jquery,    DatePicker (   )?

+3
4

, - :

private static readonly SortedList<int, string> _registeredScriptIncludes = new SortedList<int, string>();

    public static void RegisterScriptInclude(this HtmlHelper htmlhelper, string script)
    {
        if (!_registeredScriptIncludes.ContainsValue(script))
        {
            _registeredScriptIncludes.Add(_registeredScriptIncludes.Count, script);
        }
    }

    public static string RenderScript(this HtmlHelper htmlhelper, string script)
    {
        var scripts = new StringBuilder();
        scripts.AppendLine("<script src='" + script + "' type='text/javascript'></script>");
        return scripts.ToString();
    }

    public static string RenderScripts(this HtmlHelper htmlhelper)
    {
        var scripts = new StringBuilder();
        scripts.AppendLine("<!-- Rendering registered script includes -->");
        foreach (string script in _registeredScriptIncludes.Values)
        {
            scripts.AppendLine("<script src='" + script + "' type='text/javascript'></script>");
        }
        return scripts.ToString();
    }
+3

1) 20 - . Yahoo/Google -

, .

2) ?

, - ?

:

@Mare, , , . !

:

1) HTML .cs?

= > HTML aspx/ascx ( ) , .

. @admsteck

2) Javascript .cs?

= > Javascript .js

3) : HtmlHelper, - HtmlHelper?

= > , ( HtmlHelper), .

:

4) , ( CSS, ).

= > CSS JS .

4) :

a) + JS

b) + "" CSS?

c) , , , , , ?

, Client Dependency Framework MVC, , ( ).

"" , , , , , js + css ( )!

+5

2, -

<script type='text/javascript'>
    if (typeof jQuery == 'undefined') // test to see if the jQuery function is defined
        document.write("<script type='text/javascript' src='jquery.js'></script>");
</script>
+1

:

1: ( : ). .

2: , ;) - , .

0

Source: https://habr.com/ru/post/1743786/


All Articles