How can I serialize a client function to a json object? (similar to how kendo controls work)
This is what I have so far...
View:
@Html.TestControl(@<text>function(){ alert("test"); }</text>)
Control Helper:
public static HtmlString TestControl<TModel>(this HtmlHelper<TModel> helper, Func<object, object> onSubmit)
{
var obj = new {onSubmit = onSubmit.Invoke(null) };
var jsonObj = new JavaScriptSerializer().Serialize(obj);
return new HtmlString(string.Format("<script>var obj = {0};</script>", jsonObj));
}
Output:
<script>var obj = {"onSubmit":{}};</script>
Desired Output:
<script>var obj = {"onSubmit": function(){ alert("test"); }};</script>
I can see that the value of obj.onSubmit in the helper is the function... but how can I get the function to serialize and appear in the json object (as a function)?
UPDATE:
Using @<text> to define the anonymous function inline is preferred. We use Kendo controls with this syntax and the goal is to keep the code consistent.
Here is an example on the syntax for kendo controls: http://docs.kendoui.com/api/wrappers/aspnet-mvc/Kendo.Mvc.UI.Fluent/UploadEventBuilder