Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render script in the head section using ASP.NET MVC Razor [duplicate]

Tags:

I need to render some script in the head section of an ASP.NET MVC view. How can this be achieved?

In ASP.NET we had ContentPlaceHolders in Master. What is the MVC equivalent of implementing this?

like image 979
StringBuilder Avatar asked May 17 '13 14:05

StringBuilder


People also ask

Which is the way to render partial view using ASP.NET MVC Razor engine?

RenderPartial function to render Partial View in ASP.Net MVC Razor. The data will be fetched from database using Entity Framework and then the Partial View will be rendered using the @Html. RenderPartial function in ASP.Net MVC Razor.

How do I render a partial view in a script?

Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .

Can we add script in partial view MVC?

Mvc and System. Web. Mvc. Ajax namespaces can be combined with JavaScript and MVC partial views to create flexible interactive web pages with minimal code.

How do you render a partial view inside a view in MVC?

In order to add Partial View, you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.


1 Answers

You can use @section like this:

Master file:

@RenderSection("masterjs", required: false) 

View file:

@section masterjs {    <script type="text/javascript" src="@Url.Content("/Scripts/SomeScript.js")"></script>  } 
like image 132
user2031802 Avatar answered Nov 11 '22 02:11

user2031802