0

I was in the process of adding javascript files to site.js and the page was recognizing the scripts however I struck a wrinkle when I tried to move the validation scripts to site.js.

Site.js appears above the validation scripts which are added to the page using

    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}

Rather than having the validation scripts for the page being on the page (or is this how most people do it) how do I add say add a validation.js page etc that I might add below the above entry.. further,

If I do add another page how do I minify it (add it to GULP) and add it in the production staging environment as well..

2 Answers 2

3

It is recommended that you should put the validation script rendering into a section as:

@section Scripts {
    @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}

and then refer it in the _Layout.cshtml at the bottom of the body as:

    @RenderSection("scripts", required: false)

Then you will be able validate your forms and these JS files will appear only at that view in which you referred.

If I do add another page how do I minify it (add it to GULP) and add it in the production staging environment as well..

You will have to minify your JS files once for all of your Views (pages) and they will work.

Sign up to request clarification or add additional context in comments.

2 Comments

I already have that in the bottom of my page. I am referring to custom validation scripts in this case a custom script for validating the Australian ATO number. Its currently at the bottom of the page that uses it however I think it should be in its own file. Its under these circumstances that I ask about where do I put this custom validation script.. sorry of the question was not clear.
If it is in the validation section, modify the order in which files get rendered in the section and check the developer tooling of the browser that which file got missed who made the validations failed. Putting them in another file is also an option. But it is recommended to put them in the same section.
0

Ideally you'd like all scripts to be loaded at the end of the page as scripts can block the rendering of your page and make it appears slower to the user. The way we achieve this is by using sections in our layout file.

In your layout file you could add the following code:

@rendersection("scripts", required: false)

Then in your views you can add any specific scripts that they require:

@section scripts {
    @Scripts.Render("~/Scripts/Validation.js")
}

Scott Gu has a good blog post that will explain sections a bit more in depth

As for minification have you looked at using the built in bundling and minification tools that come with mvc nowadays? Here's another blog post this time by Rick Anderson that explains in all but essentially you can define different bundles which can be set to automatically bundle and minify the scripts for you. Defining a bundle is as simple as:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
             "~/Scripts/jquery-{version}.js"));

And outputing them to your page is the same as above except instead of the link to your source file you use the bundle link you defined. So outputting the above would be done like so:

@Scripts.Render("~/bundles/jquery")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.