Asp.net mvc 3 MVC视图模型>;JS ViewModel-未捕获的语法错误:意外的标记&;
当我试图在clientside viewmodel(对于knockout.js)中传递我的服务器端viewmodel时,出现了一个js错误“uncaughtsyntaxerror:Unexpected token&” 视图模型Asp.net mvc 3 MVC视图模型>;JS ViewModel-未捕获的语法错误:意外的标记&;,asp.net-mvc-3,knockout.js,Asp.net Mvc 3,Knockout.js,当我试图在clientside viewmodel(对于knockout.js)中传递我的服务器端viewmodel时,出现了一个js错误“uncaughtsyntaxerror:Unexpected token&” 视图模型 public class InvoiceViewModel { public Factuur Factuur { get; set; } public List<Factuurlijn> Factuurlijnen { get;
public class InvoiceViewModel
{
public Factuur Factuur { get; set; }
public List<Factuurlijn> Factuurlijnen { get; set; }
}
但是没有成功。。。然后我在我的js错误中看到了这一点
错误:无效的属性id
var initialDataJS = {"Factuur":{"factuur_id":0,"factuur_nummervoorvoegsel":"2012","factuur_nummer":1,"factuur_type":"F","bedrijf_id":0,"factuur_naam":null,"factuur_notities":null,"factuur_details":null,"factuur_datum":"\/Date(1335443889648)\/","factuur_vervaldatum":"\/Date(1336307889648)\/","factuur_kortingspercentage":0,"factuur_betaald":false,"factuur_bedrijf_naam":null,"factuur_bedrijf_adres":null,"factuur_bedrijf_postcode":null,"factuur_bedrijf_gemeente":null,"factuur_bedrijf_land":null,"factuur_bedrijf_tel":null,"factuur_bedrijf_fax":null,"factuur_bedrijf_gsm":null,"factuur_bedrijf_email":null,"factuur_bedrijf_website":null,"factuur_bedrijf_btw":null,"factuur_deleted":false,"bedrijf":null,"bedrijfReference":{"Value":null,"EntityKey":null,"RelationshipName":"ScotaModel.facturen_ibfk_1","SourceRoleName":"facturen","TargetRoleName":"Bedrijf","RelationshipSet":null,"IsLoaded":false},"factuurlijnen":[],"EntityState":1,"EntityKey":null},"Factuurlijnen":[{"factuurlijn_id":0,"factuur_id":0,"factuurlijn_item":"","factuurlijn_aantal":0,"factuurlijn_prijs":0,"factuurlijn_btwbedrag":0,"factuurlijn_btwpercentage":0,"factuurlijn_datum":"\/Date(-62135596800000)\/","factuurlijn_volgorde":null,"factuurlijn_deleted":false,"facturen":null,"facturenReference":{"Value":null,"EntityKey":null,"RelationshipName":"ScotaModel.factuurlijnen_ibfk_1","SourceRoleName":"factuurlijnen","TargetRoleName":"facturen","RelationshipSet":null,"IsLoaded":false},"EntityState":1,"EntityKey":null}]}
有人能帮我吗?换衣服
var initialDataJS = @(initialData)
到
来自菲尔·哈克
Razor正在尝试安全地编码所有这些引号,但实际上Javascript需要它们。您需要替换引号字符。我就是这么做的
var jsonStr = '@(Model.JSONData)';
var json = JSON.parse(jsonStr.replace(/"/g, '"'));
viewModel = new JsModel(json);
ko.applyBindings(viewModel);
所以在本例中,您将服务器中的数据保存为jsonStr变量中的字符串,然后在regEx的帮助下替换双引号并对其进行解析。这将创建一个json对象 我想我在填充模型时犯了一个错误。invoicelines必须直接位于invoice对象下方,而不是单独的。但现在我看到“序列化对象X.Models.Factuur时检测到循环引用”嗯,不太好。无法序列化使用EF自动创建的对象,因为定义一对多关系时,单侧对象包含所有多侧对象的集合,同时多侧对象中的每个对象都有一个指向其相关单侧对象的指针。这将创建一个循环引用父对象->集合->子对象->父对象。无法序列化包含循环引用的对象图。为了使序列化工作,对象图必须是树。一般来说,这对于传递给视图的所有对象都是一个很好的实践。您是否找到了问题的解决方案?
var initialDataJS = {"Factuur":{"factuur_id":0,"factuur_nummervoorvoegsel":"2012","factuur_nummer":1,"factuur_type":"F","bedrijf_id":0,"factuur_naam":null,"factuur_notities":null,"factuur_details":null,"factuur_datum":"\/Date(1335443889648)\/","factuur_vervaldatum":"\/Date(1336307889648)\/","factuur_kortingspercentage":0,"factuur_betaald":false,"factuur_bedrijf_naam":null,"factuur_bedrijf_adres":null,"factuur_bedrijf_postcode":null,"factuur_bedrijf_gemeente":null,"factuur_bedrijf_land":null,"factuur_bedrijf_tel":null,"factuur_bedrijf_fax":null,"factuur_bedrijf_gsm":null,"factuur_bedrijf_email":null,"factuur_bedrijf_website":null,"factuur_bedrijf_btw":null,"factuur_deleted":false,"bedrijf":null,"bedrijfReference":{"Value":null,"EntityKey":null,"RelationshipName":"ScotaModel.facturen_ibfk_1","SourceRoleName":"facturen","TargetRoleName":"Bedrijf","RelationshipSet":null,"IsLoaded":false},"factuurlijnen":[],"EntityState":1,"EntityKey":null},"Factuurlijnen":[{"factuurlijn_id":0,"factuur_id":0,"factuurlijn_item":"","factuurlijn_aantal":0,"factuurlijn_prijs":0,"factuurlijn_btwbedrag":0,"factuurlijn_btwpercentage":0,"factuurlijn_datum":"\/Date(-62135596800000)\/","factuurlijn_volgorde":null,"factuurlijn_deleted":false,"facturen":null,"facturenReference":{"Value":null,"EntityKey":null,"RelationshipName":"ScotaModel.factuurlijnen_ibfk_1","SourceRoleName":"factuurlijnen","TargetRoleName":"facturen","RelationshipSet":null,"IsLoaded":false},"EntityState":1,"EntityKey":null}]}
var initialDataJS = @(initialData)
var initialDataJS = @Html.Raw(initialData)
var jsonStr = '@(Model.JSONData)';
var json = JSON.parse(jsonStr.replace(/"/g, '"'));
viewModel = new JsModel(json);
ko.applyBindings(viewModel);