Skip to content

Instantly share code, notes, and snippets.

@AlexShkor
Created May 9, 2013 11:47
Show Gist options
  • Save AlexShkor/5547004 to your computer and use it in GitHub Desktop.
Save AlexShkor/5547004 to your computer and use it in GitHub Desktop.
HtmlHelper for applying Knockout model
using Newtonsoft.Json;
namespace System.Web.Mvc
{
public static class HtmlHelperExt
{
private const string BaseApplyModelScriptString = @"<script type=""text/javascript"">
$(function () {{
var model = new AllMessagesModel({0});
ko.applyBindings(model);
}});
</script>";
public static IHtmlString ApplyModel(this HtmlHelper page, object model)
{
return new MvcHtmlString(string.Format(BaseApplyModelScriptString, JsonConvert.SerializeObject(model)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment