C# 有没有一种方法可以自动将视图模型对象映射到javascript对象?
这个问题类似,但没有结论,也没有自我接受的“否”答案: Darin的回答是将对象映射到Json的一个很好的例子,尽管我不确定它是否适用于复杂的模型: 这是我所看到的一个例子。鉴于此设置: 视图模型:C# 有没有一种方法可以自动将视图模型对象映射到javascript对象?,c#,javascript,asp.net-mvc-3,razor,C#,Javascript,Asp.net Mvc 3,Razor,这个问题类似,但没有结论,也没有自我接受的“否”答案: Darin的回答是将对象映射到Json的一个很好的例子,尽管我不确定它是否适用于复杂的模型: 这是我所看到的一个例子。鉴于此设置: 视图模型: public class Avm { public int id { get; set; } public string name { get; set; } public Foo Foo { get; set; } } public class Foo { public int id {
public class Avm
{
public int id { get; set; }
public string name { get; set; }
public Foo Foo { get; set; }
}
public class Foo
{
public int id { get; set; }
public string description { get; set; }
public List<Bar> Bars { get; set; }
}
public class Bar
{
public int id { get; set; }
public string metal { get; set; }
}
这是否已经存在于框架中?在这里使用像JSON这样的序列化过程会更好吗?如果是这样的话,会使用什么类型的钩子将JSON绑定到这个场景中
在基于viewmodel的javascript中创建复杂模型的最佳实践方法是什么。这听起来不像是在尝试提供JSON,正如在对as的回复中所指出的那样,这纯粹是为了数据传输,而更像是在尝试使用MVC3为JSONP提供服务 使用MVC3为JSONP提供服务是很有可能的
.您想要的是来自knockout.js库的类似内容吗 JSON对象示例:
var data = {"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
示例映射
var viewModel = ko.mapping.fromJSON(data);
您可以将viewmodel序列化到JSON服务器端,然后使用它自动生成javascript viewmodel。每次从服务器接收数据时,只需调用ko.mapping.fromJSON(数据)调用
链接到文档(非常好):
如果这是你想要的,请告诉我
编辑:这里有一个指向一个好的JSON序列化库的链接,您可以通过Nuget安装它来快速演示 我不喜欢使用JavaScriptSerializer 不确定如何在此处发布Gist,因此以下是带有相关代码段的Gist链接: Index.cshtml
@model JSSerializerSample.Models.IndexViewModel
@{
ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
<div id="userName"></div>
<div id="message"></div>
<div id="complexProperty1"></div>
<script>
$(function () {
var options = @Html.Raw(Model.AsJson());
$("#userName").html(options.UserName);
$("#message").html(options.Message);
$("#complexProperty1").html(options.ComplexProperty.ComplexProperty1);
});
</script>
IndexViewModel.cs
namespace JSSerializerSample.Models
{
public class ComplexViewModel
{
public string ComplexProperty1 { get; set; }
}
}
namespace JSSerializerSample.Models
{
public class IndexViewModel : BaseViewModel
{
public string Message { get; set; }
public string UserName { get; set; }
public ComplexViewModel ComplexProperty { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
namespace JSSerializerSample.Models
{
public abstract class BaseViewModel
{
public string AsJson()
{
var serializer = new JavaScriptSerializer();
return serializer.Serialize(this);
}
}
}
BaseViewModel.cs
namespace JSSerializerSample.Models
{
public class ComplexViewModel
{
public string ComplexProperty1 { get; set; }
}
}
namespace JSSerializerSample.Models
{
public class IndexViewModel : BaseViewModel
{
public string Message { get; set; }
public string UserName { get; set; }
public ComplexViewModel ComplexProperty { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
namespace JSSerializerSample.Models
{
public abstract class BaseViewModel
{
public string AsJson()
{
var serializer = new JavaScriptSerializer();
return serializer.Serialize(this);
}
}
}
完整的代码示例可以在这里下载:我正要发布完全相同的内容。Knockout非常棒,它可以将模型映射到javascript对象。但是knockout实际上将创建的模型映射为可观察的所有属性和数组,如果您不希望这种行为,可以使用
var unmap=ko.mapping.toJS(viewModel)获得一个简单的JavaScript对象顺便说一句,我想你的意思是var viewModel=ko.mapping.fromJS(数据)
而不是var viewModel=ko.mapping.fromJSON(数据)因为后一个函数是用来处理字符串的,而不是JavaScript objectsTrue,所以他的函数实际上不需要反序列化服务器返回的JSON,所以两者都可以工作。取决于他喜欢什么。谢谢你的建议和帮助。我知道当你发布这篇文章时,我们并没有完全弄明白如何解决循环引用问题,但我确实弄明白了。解决方案是在运行时动态更改延迟加载和代理创建配置。以下是我添加到存储库中的内容:if(DisableProxy){context.Configuration.ProxyCreationEnabled=false;context.Configuration.LazyLoadingEnabled=false;}