Class JsonVariables

JsonVariables provides an easy way to embed server side objects and values as JSON into client side JavaScript code.

You can use the dynamic Add() method to add properties to an object with each added value becoming a property on the rendered JSON object. Pass simple values, or complex objects and lists to easily push server side static data into client side JavaScript code.

The static ToJsonString() and ToJsonHtmlString() methods also provide easy self contained JSON serialization for any object.

This component produces either straight string or HtmlString output when used directly using the ToString() or HtmlString() methods for use in ASP.NET MVC or Web Pages, or can be used as WebForms control that automatically handles embedding of the script and deserialization of return values on the server.

This component supports:<

    > <
  • > Creating individual client side variables <
  • > Dynamic values that are 'evaluated' in OnPreRender to pick up a value <
  • > Creating properties of ClientIDs for a given container <
  • > Changing the object values and POSTing them back on Postback <
>

You create a script variables instance and add new keys to it: <> var scriptVars = new JsonVariables("scriptVars");

// Simple value scriptVars.Add("userToken", UserToken);

var book = new AmazonBook(); book.Entered = DateTime.Now;

// Complex value marshalled scriptVars.Add("emptyBook", book);

In client code you can then access these variables: <>$( function() { alert(scriptVars.book.Author); alert(scriptVars.author); alert( $("#" + scriptVars.txtAmazonUrlId).val() ); });<>

System.Object
  Westwind.Web.JsonVariables
public class JsonVariables : object

Class Members

MemberDescription

Constructor

Add

Adds a property and value to the client side object to be rendered into JavaScript code. VariableName becomes a property on the object and the value will be properly converted into JavaScript Compatible JSON text.

public void Add(string variableName,     object value)

public void Add(IDictionary<Object,Object> values)

ToHtmlString

Returns the script as an HTML string. Use this version with AsP.NET MVC to force raw unencoded output in Razor:

public HtmlString ToHtmlString(bool noVarStatement)

ToJson

Outputs the variable data as a raw JSON object

public string ToJson()

ToJsonHtmlString

Serialization helper that can be used to easily embed JSON values into Razor pages. Returns an HTML string that doesn't require @Html.Raw()

public static HtmlString ToJsonHtmlString(object val)

public HtmlString ToJsonHtmlString()

ToJsonString

Serialization helper that can be used to easily embed JSON values into ASP.NET pages. For Razor page embedding use ToJsonHtmlString instead.

public static string ToJsonString(object val)

ToPropertyDictionaryHtmlString<TKey,TValue>

public static HtmlString ToPropertyDictionaryHtmlString<TKey,TValue>(Dictionary<TKey,TValue> val)

ToPropertyDictionaryString<TKey,TValue>

public static string ToPropertyDictionaryString<TKey,TValue>(Dictionary<TKey,TValue> val)

ToString

Returns the rendered JavaScript for the generated object and name. Note this method returns only the generated object, not the related code to save updates.

public string ToString(bool noVarStatement)

ClientObjectName

The name of the object generated in client script code

Requirements

Namespace: Westwind.Web
Assembly: westwind.web.dll

© West Wind Technologies, 1996-2016 • Updated: 12/12/15
Comment or report problem with topic