Skip to content

Instantly share code, notes, and snippets.

@linuxbender
Created May 17, 2011 12:13
Show Gist options
  • Save linuxbender/976361 to your computer and use it in GitHub Desktop.
Save linuxbender/976361 to your computer and use it in GitHub Desktop.
MVC 3 - Razor - C# -Custom helper: @Html.Script
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="page">
<header>
<div id="title">
<h1>Helper Demo</h1>
</div>
<div id="logindisplay">&nbsp;</div>
<nav>
<ul id="menu">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
</ul>
</nav>
</header>
<section id="main">
@RenderBody()
</section>
<footer>
@Html.Label(DateTime.Now.Year.ToString())&copy;kinesys.ch
</footer>
</div>
@Html.Script("jquery-1.6.1.min.js")
</body>
</html>
using System.Web.Mvc;
namespace Starwolf.ch.Helpers
{
public static class MyHelpers
{
public static MvcHtmlString Script(this HtmlHelper helper, string src)
{
// <script src="@Url.Content("~/Scripts/jquery-1.6.1.min.js")" type="text/javascript"></script>
var builder = new TagBuilder("script");
builder.MergeAttribute("src", "/Scripts/" + src);
builder.MergeAttribute("type", "text/javascript");
return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
}
}//end of class
}//end of namespace
<add namespace="Starwolf.ch.Helpers" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment