Skip to content

Instantly share code, notes, and snippets.

#Controller
def HomeController < ApplicationController
def index
gon.myVar = 100
end
end
#home.html.erb
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalFilters.Filters.Add(new NGonActionFilterAttribute());
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.NGon.SomeValue = 100;
return View();
}
}
<script type="text/javascript">
$(function () {
$("#button").click(function () {
alert(ngon.SomeValue);
});
}); </script>
public class HomeController : Controller
{
public ActionResult Index()
{
var person = new Person { FirstName = "John", LastName = "Doe", Age = 30 };
ViewBag.NGon.Person = person;
return View();
}
}
<script type="text/javascript">
$(function () {
$("#button").click(function () {
var person = ngon.Person;
var div = $("#output");
div.html('');
div.append("FirstName: " + person.FirstName);
div.append(", LastName: " + person.LastName);
div.append(", Age: " + person.Age);
});
public class NGonActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Controller.ViewBag.NGon = new ExpandoObject();
base.OnActionExecuting(filterContext);
}
}
public static class HtmlHelperExtensions
{
public static IHtmlString IncludeNGon(this HtmlHelper helper, string @namespace = "ngon")
{
var viewData = helper.ViewContext.ViewData;
if (viewData == null)
{
return MvcHtmlString.Empty;
}
<script type="text/javascript">window.ngon={};ngon.Person={"FirstName":"John","LastName":"Doe","Age":30};</script>
public static class RandomStreamGenerator
{
private static Random _random = new Random();
public static void GenerateIndefinitley<T>(Func<T> func, Action<T> onNext)
{
GenerateIndefinitley(func, 100, 1000, onNext);
}
public static void GenerateIndefinitley<T>(Func<T> func, int minMilliseconds, int maxMilliseconds, Action<T> onNext)