I am a Java developer, new to .NET. I am working on a .NET MVC2 project where I want to have a partial view to wrap a widget. Each JavaScript widget object has a JSON data object that would be populated by the model data. Then methods to update this data are bound to events when data is changed in the widget or if that data is changed in another widget.

代码是这样的:

MyController:

virtual public ActionResult DisplaySomeWidget(int id) {
  SomeModelView returnData = someDataMapper.getbyid(1);

  return View(myview, returnData);
}

myview.ascx:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SomeModelView>" %>

<script type="text/javascript">
  //creates base widget object;
  var thisWidgetName = new Widget();

  thisWidgetName.updateTable = function() {
    //  UpdatesData
  };
  $(document).ready(function () {
    thisWidgetName.data = <% converttoJSON(model) %>
    $(document).bind('DATA_CHANGED', thisWidgetName.updateTable());
  });
</script>

<div><%:model.name%></div>

我不知道的是如何将数据发送到SomeModelView多个位置,然后能够使用这些数据填充小部件,并将其转换为JSON.我在控制器中看到了一些真正简单的方法,但在视图中没有看到.我想这是一个基本的问题,但我已经花了几个小时试图把这个问题弄清楚.

推荐答案

In mvc3 with razor @Html.Raw(Json.Encode(object)) seems to do the trick.

Json相关问答推荐

PostgreSQL 12.17从JSON数组提取元素

抓取低于w jolt的对象级别

将JSON输入子数组转换为字符串顺序列表输出

如何在VegaLite中应用Times New Roman,CaliBiri字体

JQ如何获取特定子元素的所有父母

无法从JSON解析ZonedDateTime,但可以使用格式化程序很好地解析

用于参考的Jolt变换

Jolt-在数组列表中插入新的字段

将PNG图像保存为Python中的JSON文件

PowerShell:使用JSON原生的Short命令处理JSON?

迭代powershell双维json对象

如何对未知/变量键的字典进行编码?

将不带正文的 CURL POST 转换为 RESTRequest Delphi 代码 / 为 Dropbox API /get_current_account 端点编写 Delphi 代码

jq - 将父键值提取为子元素旁边的逗号分隔值

Scala - 在构建 Json 时无法删除 Key -> value "{}" 大括号的双引号

如何使用 Kotlin + Jackson 将 JSON 反序列化为 List

在 C# 中解析 Json rest api 响应

通过 RestAssured 中的 JsonPath 访问匿名数组的元素

Sequelize - 如何仅返回数据库结果的 JSON 对象?

有没有一种快速的方法可以在文本编辑器中将 JavaScript 对象转换为有效的 JSON?