0

I'm lost with so many examples and solutions out here I cannot seem to find anything that makes sense to me. So, I'm going to take a baby step and ask How do you move JavaScript file reference from existing view (that will now be a Partial view) to Main view?

Existing Report View

This will become my partial view, so I cannot use the @Scripts.Render("~/bundles/App/UsageReport") anymore. Once this is rendered as a partial page I must include this JavaScript file in the Main page.

@{
    ViewBag.Title = "Usage Report";
}
<main id="usageData">
    .
    .
    .
    <div id="loading">
    </div>
</main>

@section scripts{

    @Scripts.Render("~/bundles/App/UsageReport")

    <script type="text/javascript">
        $(function () { UsageReport.initModule() });
    </script>
}

Main View

Where / How do I include UsageReport.js in the Main view shown below?

@{
    ViewBag.Title = "Main View";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<main id="mainView">
    .
    .
    .
    <div id="loading">
    </div>
</main>

@section scripts{

    @Scripts.Render("~/bundles/App/MainView")

    <script type="text/javascript">
            $(function () { MainView.initModule('@ViewBag.Name') });
    </script>
}

I'll include any other code you need but I'm not sure what is important to add for this dumbass question.

12
  • 1
    Just add the Scripts.Render("~/bundles/App/UsageReport") in the @section scripts{ of your main view. It not clear what UsageReport.initModule() does, but if the partial is loaded dynamically, you would need to call that in the success call back of the ajax call Commented Feb 6, 2018 at 21:43
  • This is a great start. Thank you. Commented Feb 6, 2018 at 21:49
  • In the ajax call, what should the url look like? Using the filepath from BundleConfig ("~/Scripts/app/UsageReport/AggregatedUsageReport.js") is not working and neither does the path that was in the @Scripts.Render("~/bundles/App/UsageReport") Commented Feb 6, 2018 at 23:22
  • OK. I figured out the url. It is where the file exists relative to the location of the view file (url: "../../Scripts/app/UsageReport/AggregatedUsageReport.js") Commented Feb 6, 2018 at 23:59
  • YAY!!!! That worked! :-) Commented Feb 7, 2018 at 0:05

0

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.