JsonModel and JsonModelFor Helpers

The JSonModel and JSonModel for helpers are useful when we would like to transfer a part of the Server Side ViewModel to the Client Side without using the knockout bindings of the Client Blocks. This might be useful if we need to turn an exixting Javascript control/widget that doesn't support knockout bindings into an Mvc Control. This way, if the javascript control needs to be bound to a javascript model, we can take it from the main Server Side ViewModel: 

MvcHtmlString JsonModelFor<M, T>(this HtmlHelper<M> htmlHelper, Expression<Func<M, T>> expression, IDictionary<string, object> htmlAttributes=null, bool translateBack = false)

MvcHtmlString JsonModel<M,T>(this HtmlHelper<M> htmlHelper, string name, T value, IDictionary<string, object> htmlAttributes=null, bool translateBack = false)

The object specified either with the value parameter, or with the expression parameter is serialized into jSon and put into an Hidden Field. The name of the Hidden filed, is not easy to compute, so it is better to use some CSS class into the htmlAttributes parameter to select it from javascript.

All Datetime fields are converted into ticks and put into integer JSon properties, since the standard jSon format has no date or time types.

If the translateback parameter is set to true, the javascript control can return a modified json object into the same hidden field. When the form is posted such a result object will be de-serialized, validated and inserted into the whole Server Side ViewModel of the receiving action method.

Last edited Jun 22, 2014 at 10:47 AM by frankabbruzzese, version 4

Comments

No comments yet.