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.TestCo
You are confusing a function on the server with a function on the client. You're actually executing onSubmit on the server and the result of that is being put into your object which you serialize into json.
Instead:
Make your 2nd parameter to TestControl a string. Also, don't serialize an object. Instead, create your json manually.
public static HtmlString TestControl(this HtmlHelper helper, string onSubmit)
{
string jsonObj = String.Format("{{ \"onSubmit\": {0} }}", onSubmit);
return new HtmlString(string.Format("", jsonObj));
}
Then you can use:
@Html.TestControl("function(){ alert('test'); }")
Your jsonObj will be:
{ "onSubmit": function(){ alert('test'); } }
and finally, your TestControl()
method will return