Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is @section scripts and what it is used for

Tags:

asp.net-mvc

I have downloaded a chat example from the Microsoft website. I have been following several tutorials but I have never seen the @section script{} before I have done scripts without this block of c# code (@section script{}) and it seem to work fine but in this instance of the chat application using signal R when I do take the scripts outside the block it does not work.

@section scripts { <!--Script references. --> <!--The jQuery library is required and is referenced by default in _Layout.cshtml. --> <!--Reference the SignalR library. --> <script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script> <!--Reference the autogenerated SignalR hub script. --> <script src="~/signalr/hubs"></script> <!--SignalR script to update the chat page and send messages.--> <script>     $(function () {         // Reference the auto-generated proxy for the hub.         var chat = $.connection.chatHub;         // Create a function that the hub can call back to display messages.         chat.client.addNewMessageToPage = function (name, message) {             // Add the message to the page.             $('#discussion').append('<li><strong>' + htmlEncode(name)                 + '</strong>: ' + htmlEncode(message) + '</li>');         };         // Get the user name and store it to prepend to messages.         $('#displayname').val(prompt('Enter your name:', ''));         // Set initial focus to message input box.         $('#message').focus();         // Start the connection.         $.connection.hub.start().done(function () {             $('#sendmessage').click(function () {                 // Call the Send method on the hub.                 chat.server.send($('#displayname').val(), $('#message').val());                 // Clear text box and reset focus for next comment.                 $('#message').val('').focus();             });         });     });     // This optional function html-encodes messages for display in the page.     function htmlEncode(value) {         var encodedValue = $('<div />').text(value).html();         return encodedValue;     } </script> } 
like image 429
Decoder94 Avatar asked Jun 09 '16 13:06

Decoder94


People also ask

What does @section scripts do?

That is, it guarantees that scripts will load after all page contents which is essential. By doing this, you actually make sure necessary elements for your JavaScript code have loaded already and also it is a matter of performance.

What is @RenderSection scripts required false?

On your layout page, if required is set to false : @RenderSection("scripts", required: false) , when the page renders and the user is on the about page, contacts. js won't render. If required is set to true : @RenderSection("scripts", required: true) , When page renders and user is on the About page, contacts.

HOW include script in MVC application?

Go to Views -> Shared -> _Layout. cshtml file and add the render code. Make sure to register the custom javascript file after the jquery bundle since we are going to use jquery inside our js file. Otherwise we will get a jquery error and also register this before the script RenderSection.


1 Answers

A section allows you to add something in a view which will be added in the layout. ie:-

view

@section scripts {      <script>        alert('foo');      </script>  } 

layout

@RenderSection("scripts", false) 

now this named section scripts will be rendered where you have specified in the layout.

@RenderSection also has 2 signatures:-

public HelperResult RenderSection(string name) // section required in the view public HelperResult RenderSection(string name, bool required) 
like image 168
BenG Avatar answered Oct 28 '22 03:10

BenG