Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This is the code which I have in my partial view

@model Contoso.MvcApplication.Models.Exercises.AbsoluteArithmetic  @using(Html.BeginForm()) { <div>     <span style="width: 110px; float:left; text-align:center; font-size:2.5em;">@Model.Number1</span>     <span style="width: 110px; float:left; text-align:center; font-size:2.5em;">+</span>     <span style="width: 110px; float:left; text-align:center; font-size:2.5em;">@Model.Number2</span>     <span style="width: 110px; float:left; text-align:center; font-size:2.5em;">=</span>     <span>             @Html.EditorFor(model => model.Result)             @Html.ValidationMessageFor(model => model.Result)     </span> </div> }  @section Scripts {     @Scripts.Render("~/bundles/jqueryval") } 

Please note at the bottom of my code, I've got a @section, and I realized that it's not running if I set a breakpoint there. If I move that line in the _Layout.cshtml it works well, but that's not the idea.

How can I tell to MVC4 in a partial razor view that I want to add that library?

like image 963
Darf Zon Avatar asked Jan 01 '13 20:01

Darf Zon


People also ask

How do you call a partial view from another view?

To create a partial view, right click on the Shared folder -> click Add -> click View.. to open the Add View popup, as shown below. You can create a partial view in any View folder. However, it is recommended to create all your partial views in the Shared folder so that they can be used in multiple views.

How do you use partial views?

To create a partial view, right-click on view -> shared folder and select Add -> View option. In this way we can add a partial view. It is not mandatory to create a partial view in a shared folder but a partial view is mostly used as a reusable component, it is a good practice to put it in the "shared" folder.


1 Answers

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

like image 88
Nishanth Shaan Avatar answered Sep 25 '22 15:09

Nishanth Shaan