【发布时间】:2015-10-29 05:20:57
【问题描述】:
当从 MVC 后端请求视图时,我希望收到比视图内容更多的信息:
public class CustomJsonViewResult
{
public string Subject { get; set; }
public ActionResult View { get; set; }
}
随着时间的推移,上述类会变得更加复杂。
这将在 Controller 方法中创建,如下所示:
public JsonResult Person()
{
return Json(new CustomJsonViewResult
{
View = View(),
Subject = "Update an existing person"
}, JsonRequestBehavior.AllowGet);
}
将添加 BL 逻辑来确定主题,而不是现在存在的静态值。
在接收客户端我有一个 AngularJS 指令,其链接如下:
link: function (scope, element, attrs) {
screensService.GetPartialView(attrs.view).then(function (viewData) {
var linkFunc = $compile(viewData.View);
var content = linkFunc(scope);
element.append(content);
});
}
这在我直接返回 ActionResult 时有效,但在当前设置中出现此错误:
typeError: Cannot read property 'ownerDocument' of undefined
at Function.Sizzle.contains (jquery-2.1.4.js:1430)
at Function.jQuery.extend.buildFragment (jquery-2.1.4.js:5147)
at jQuery.fn.extend.domManip (jquery-2.1.4.js:5387)
at jQuery.fn.extend.append (jquery-2.1.4.js:5218)
at PartialViewLoaderDirective.js:27
at processQueue (angular.js:14569)
at angular.js:14585
at Scope.$get.Scope.$eval (angular.js:15848)
at Scope.$get.Scope.$digest (angular.js:15659)
at Scope.$get.Scope.$apply (angular.js:15953)
如果我将 viewData 记录到控制台,我会得到:
Object {Subject: "Update an existing person", View: Object}
Subject: "Update an existing person"
View: Object
MasterName: ""
Model: null
TempData: Array[0]
View: null
ViewBag: Object
ViewData: Array[0]
ViewEngineCollection: Array[2]
ViewName: ""
jQuery21400306884015444666152: 19
__proto__: Object
__proto__: Object
总结
知道这个 Cannot read property 'ownerDocument' of undefined 错误的原因是什么吗?据我所知,一切都按预期可用。
【问题讨论】:
-
参考this answer 为 Jsonresult 添加视图
标签: javascript .net json asp.net-mvc angularjs