Skip to content

Instantly share code, notes, and snippets.

@StacyGay
Created November 1, 2012 16:14
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 StacyGay/3994708 to your computer and use it in GitHub Desktop.
Save StacyGay/3994708 to your computer and use it in GitHub Desktop.
Utilities methods for getting data to javascript front end
namespace Utilities.Data
{
public static class Extensions
{
public static string ToJson(this Object obj)
{
return (new JavaScriptSerializer()).Serialize(obj);
}
}
public class DataHelper
{
public static string ObjectToHtmlData<T>(T obj)
{
string htmlDataAttributes = "";
foreach(var dataMember in obj.GetType().GetProperties())
htmlDataAttributes += "data-"+dataMember.Name+"=\""+dataMember.GetValue(obj,null)+"\" ";
return htmlDataAttributes;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment