MVC + RegisterClientScriptInclude / RegisterClientScriptBlock

Is there any MVC equivalent to Page.ClientScripts.RegisterClientScriptInclude and Page.ClientScripts.RegisterClientScriptBlock?

I am looking to create Partial Views which may very well be referenced multiple times from the same main view.

These views might very well have their own script requirements but it seems wasteful to write includes on every potential main view or master page.

Also I feel that it would be sensibel to have the Partial view call some javascript once to initialise all of it's sibling views.

For example...

$('input[alt=date]').datepicker();

...is something which I should really only have to call once, but only if a partial view has placed such a hmtl control in the view.

So any ideas how I can achieve the old Page.ClientScripts.RegisterClientScriptInclude and Page.ClientScripts.RegisterClientScriptBlock functionality within the ASP.Net MVC Framework

Answers:

Answer

You can use Spark view engine, that has "once" attribute for this purpose. E.g.

<content name="MasterPageHead">
  <script once="CustomScriptUniqueIdName" ... />
</content>

When used in a view or a partial, script (or this can be any other tag, e.g. div) will only be included once in the master page section called MasterPageHead (which you can place in <head>).

Not sure if this will be helpful for you, but you asked for a MVC solution, not WebForms solution.

Answer

I've done this with an extension method on the HtmlHelper Html.RegisterJavascript( string ). I create a key, "____javascript____", in the ViewData dictionary. This holds a dictionary of javascript entries keyed off of a digest of the javascript (to prevent duplicates).

The problem is that these registrations happen after the header has been processed, so script tags have to go at the end of the file. Before the body end tag, I have a call to Html.RenderJavascript(); (my method).

I couldn't find a better way, but I have to imagine a more clever solution is possible.

Answer

1- Add Extentions methods class

public static class Extentions
    {
public static void AddRegisterClientsSript(this HtmlHelper helper, string key, string script)
        {
            Dictionary<string, string> scripts;
            if (helper.ViewBag.RegisterClientsSript != null)
            {
                scripts = (Dictionary<string, string>)helper.ViewBag.RegisterClientsSript;
            }
            else
            {
                scripts = new Dictionary<string, string>();
                helper.ViewBag.RegisterClientsSript = scripts;
            }

            if (!scripts.ContainsKey(key))
            {
                scripts.Add(key, script);
            }

        }

        public static MvcHtmlString RegisterClientsSript(this HtmlHelper helper)
        {
            var outScripts = new StringBuilder();

            if (helper.ViewBag.RegisterClientsSript != null)
            {
                var scripts = (Dictionary<string, string>)helper.ViewBag.RegisterClientsSript;
                foreach (string script in scripts.Values)
                {
                    outScripts.AppendLine(script);
                }
            }

            return MvcHtmlString.Create(outScripts.ToString());
        }

        public static MvcHtmlString Paging(this HtmlHelper helper, string uniqId)
        {
            helper.AddRegisterClientsSript("jquerypaginatecss", @"<link href=""/Content/Paginate/jquery.paginate.css"" rel=""stylesheet"" />");
            helper.AddRegisterClientsSript("jquerypaginatejs", @"<script src=""/Content/Paginate/jquery.paginate.js""></script>");
        }
}

2- Add the namespace in page and use it

@using Mirak.Ui.Components

@section scripts{
   @Html.RegisterClientsSript()
}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.