How to Put Javascript into an ASP.NET MVC View

Posted by Maxim Z. on Stack Overflow See other posts from Stack Overflow or by Maxim Z.
Published on 2010-04-02T01:04:37Z Indexed on 2010/04/02 1:13 UTC
Read the original article Hit count: 631

I'm really new to ASP.NET MVC, and I'm trying to integrate some Javascript into a website I'm making as a test of this technology.

My question is this: how can I insert Javascript code into a View?

Let's say that I start out with the default ASP.NET MVC template. In terms of Views, this creates a Master page, a "Home" View, and an "About" view. The "Home" View, called Index.aspx, looks like this:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%= Html.Encode(ViewData["Message"]) %></h2>
    <p>
        To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
    </p>
    <p>Welcome to this testing site!</p>
</asp:Content>

Adding a <script> tag here didn't work. Where and how should I do it?

P.S.: I have a feeling I'm missing something very basic... Thanks in advance!

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about asp.net-mvc