Skip to content

Instantly share code, notes, and snippets.

@adamtal3
Forked from benpriebe/readme.md
Last active August 29, 2016 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamtal3/92d3c5c1d2e31c491b30d01b92c918b8 to your computer and use it in GitHub Desktop.
Save adamtal3/92d3c5c1d2e31c491b30d01b92c918b8 to your computer and use it in GitHub Desktop.
Transferring Server data to JavaScript with initial Page Request

Transferring Server data to JavaScript with initial Page Request

Often you want to return data with your call to retrieve a web page/control from ASP.NET MVC so that it can be accessed via JavaScript as a ViewModel (using KnockoutJS or equivalent).

Typically, you make a server call to retrieve the web page using the standard ASP.NET controller action and then make a AJAX call to go retrieve the model to populate your JavaScript ViewModel.

Wouldn't it be nice if you could send back the model with the page request and avoid a separate AJAX call?

How

Using the ViewBag, a HTML Helper method and some JSON serialization formatters we can easily take a C# model and serialize it down with a page request to be actioned by JavaScript.

Start by pushing the data model onto the ViewBag.

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.Name = "Donkey Kong";
        
        return View();
    }

Serialize the ViewBag into a namespaced/global JavaScript variable.

<!DOCTYPE html>
<html lang="en">
  <head>
    ...
      <script type="text/javascript">
        window.viewBag = @Html.ToJson((object)ViewBag)   
      </script>
  </head>
  <body>
  ...
  </body>
</html>

Here is the helper method to serialize the ViewBag to JSON. It even converts the C# PascalCasing to JavaScript camelCasing.

public static class HtmlHelpers
{
    /// <summary>
    /// Converts a viewbag object to Json
    /// </summary>
    /// <param name="viewBagObject">Object to make into Json</param>
    /// <returns>Javascript object</returns>
    public static IHtmlString ToJson(this HtmlHelper html, dynamic viewBagObject)
    {
        var json = JsonConvert.SerializeObject(
            viewBagObject, 
            Formatting.Indented, 
            new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }
        );

        return html.Raw(json);
    }
}

Now you can access the model data from your JavaScript.

  var vm = window.viewBag;
  alert(vm.name);

Dependencies

You'll need to NuGet the Newtonsoft.Json library.

Keywords

Serialize, ViewModel, Knockout, JavaScript, ViewBag, Model, AJAX

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment