In mvc3 with razor @Html.Raw(Json.Encode(object))
seems to do the trick.
Well done, you've only just started using MVC and you've found its first major flaw.
You don't really want to be converting it to JSON in the view, and you don't really want to convert it in the controller, as neither of these locations make sense. Unfortunately, you're stuck with this situation.
The best thing I've found to do is send the JSON to the view in a ViewModel, like this:
var data = somedata;
var viewModel = new ViewModel();
var serializer = new JavaScriptSerializer();
viewModel.JsonData = serializer.Serialize(data);
return View("viewname", viewModel);
then use
<%= Model.JsonData %>
in your view. Be aware that the standard .NET JavaScriptSerializer is pretty crap.
doing it in the controller at least makes it testable (although not exactly like the above - you probably want to take an ISerializer as a dependency so you can mock it)
Update also, regarding your JavaScript, it would be good practice to wrap ALL the widget JS you have above like so:
(
// all js here
)();
this way if you put multiple widgets on a page, you won't get conflicts (unless you need to access the methods from elsewhere in the page, but in that case you should be registering the widget with some widget framework anyway). It may not be a problem now, but it would be good practice to add the brackets now to save yourself muchos effort in the future when it becomes a requirement, it's also good OO practice to encapsulate the functionality.
I found it to be pretty nice to do it like this (usage in the view):
@Html.HiddenJsonFor(m => m.TrackingTypes)
Here is the according helper method Extension class:
public static class DataHelpers
{
public static MvcHtmlString HiddenJsonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
return HiddenJsonFor(htmlHelper, expression, (IDictionary<string, object>) null);
}
public static MvcHtmlString HiddenJsonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
{
return HiddenJsonFor(htmlHelper, expression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString HiddenJsonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
{
var name = ExpressionHelper.GetExpressionText(expression);
var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
var tagBuilder = new TagBuilder("input");
tagBuilder.MergeAttributes(htmlAttributes);
tagBuilder.MergeAttribute("name", name);
tagBuilder.MergeAttribute("type", "hidden");
var json = JsonConvert.SerializeObject(metadata.Model);
tagBuilder.MergeAttribute("value", json);
return MvcHtmlString.Create(tagBuilder.ToString());
}
}
It is not super-sofisticated, but it solves the problem of where to put it (in Controller or in view?) The answer is obviously: neither ;)
You can use Json
from the action directly,
Your action would be something like this:
virtual public JsonResult DisplaySomeWidget(int id)
{
SomeModelView returnData = someDataMapper.getbyid(id);
return Json(returnData);
}
Edit
Just saw that you assume this is the Model
of a View so the above isn't strictly correct, you would have to make an Ajax
call to the controller method to get this, the ascx
would not then have a model per se, I will leave my code in just in case it is useful to you and you can amend the call
Edit 2 just put id into the code
@Html.Raw(Json.Encode(object)) can be used to convert the View Modal Object to JSON
Extending the great answer from Dave. You can create a simple HtmlHelper.
public static IHtmlString RenderAsJson(this HtmlHelper helper, object model)
{
return helper.Raw(Json.Encode(model));
}
And in your view:
@Html.RenderAsJson(Model)
This way you can centralize the logic for creating the JSON if you, for some reason, would like to change the logic later.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With