How to add a script in a partial view in MVC4?

前端 未结 9 1924
天涯浪人
天涯浪人 2020-12-05 09:15

This is the code which I have in my partial view

@model Contoso.MvcApplication.Models.Exercises.AbsoluteArithmetic

@using(Html.BeginForm())
{
相关标签:
9条回答
  • 2020-12-05 09:51

    There is no common solution for this issue but you can do the following simplest ways:

    1) You can create a set of extension method as the following:

    https://stackoverflow.com/a/6671766/5208058

    2) Simply move your javascript codes into a separated partial view and on your main view, render 2 partial views. One for the main partial view and the other for your scripts as the following:

    {
        // markup and razor code for Main View here
    
        @Html.Partial("Main_PartialView")
    }
    
    @section Scripts
    {
        @Html.Partial("JavaScript_PartialView")
    }
    

    Hope it helps.

    0 讨论(0)
  • 2020-12-05 09:53

    You can't render layout sections from a partial. Move the section definition to the parent page or layout.

    0 讨论(0)
  • 2020-12-05 09:56

    If you want to include specific scripts only in some partial views and avoid spreading them unnecessarily throughout your application, you can do something like this:

    Define a bundle pointing to an empty javascript file in your BundleConfig.cs:

     bundles.Add(new ScriptBundle("~/bundles/empty").Include(
                        "~/Scripts/empty.js"
                ));
    

    In the head section of your _Layout.cshtml, add this variable:

    @{
            ViewBag.AdditionalBundle = String.IsNullOrEmpty(ViewBag.AdditionalBundle) ? "~/bundles/empty" : ViewBag.AdditionalBundle;
        }
    

    In the bottom of your _Layout.cshtml, render any additional bundles you want:

    @Scripts.Render("~/bundles/lib")
        @Scripts.Render(@ViewBag.AdditionalBundle);
        @RenderSection("scripts", required: false)
    

    And finally, in the partial view in which you need any specific scripts, just add the corresponding bundle to the variable:

    ViewBag.AdditionalBundle = "~/bundles/mySpecificBundle";
    

    Partial views are rendered before the _Layout.cshtml, so you need that verification at the top of the file. It goes like this: if any partial view assigned a value to ViewBag.AdditionalBundle, then use it. Otherwise, render an empty script.

    0 讨论(0)
  • 2020-12-05 10:01

    You can add the script directly at the end of the partial view html, without script section (because script section is not rendered in partial views)

    <script language="javascript">
       // Your scripts here
       // ....
    </script>
    
    0 讨论(0)
  • 2020-12-05 10:02

    You can use @Scripts.Render("~/Scripts/my-script.js") for .js files and @Styles.Render("~/Content/my-Stylesheet.css") for css files.

    Nb: it works for a particular bundle also More details - 'http://www.asp.net/mvc/overview/performance/bundling-and-minification'

    it works on any sub-pages in razor including partial views. for more info google for the usage of these helpers

    0 讨论(0)
  • 2020-12-05 10:04

    This Stackoverflow page provided a full solution to this question: Using sections in Editor/Display templates

    TL;DR: Just add the Forloop.HtmlHelpers nuget package https://www.nuget.org/packages/Forloop.HtmlHelpers/ to your project to allow you to run Javascript from Razor Partial Views and Templates in ASP.NET MVC. I have personally used this with my MVC 5 project.

    0 讨论(0)
提交回复
热议问题