Last active
December 25, 2015 09:09
-
-
Save gregjsmith/6951917 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SomeController : Controller | |
{ | |
public ActionResult Stuff() | |
{ | |
var user = GetUser(); | |
ViewBag.User = user; | |
return View(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Helpers | |
{ | |
public static IHtmlString AssignViewData(this HtmlHelper helper) | |
{ | |
var bag = helper.ViewData; | |
var script = new TagBuilder("script"); | |
script.Attributes.Add(new KeyValuePair<string, string>("type", "text/javascript")); | |
var sb = new StringBuilder(); | |
sb.AppendFormat("window.ViewData="); | |
var d = new Dictionary<string, object>(); | |
foreach (dynamic val in bag) | |
{ | |
d.Add(val.Key, val.Value); | |
} | |
var vals = JsonSerializer.SerializeToString(d); | |
sb.Append(vals); | |
script.InnerHtml = sb.ToString(); | |
return new HtmlString(script.ToString()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Html.AssignViewData() | |
<html> | |
<p>Hello</p> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
SomeController: function($scope, $window){ | |
$scope.user = $windw.ViewData.user; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment