0

When requesting Views from the MVC backend, I would like to receive more information than just the View content:

public class CustomJsonViewResult
{
    public string Subject { get; set; }

    public ActionResult View { get; set; }
}

The above class would become complexer over time.

This would be created in a Controller method like this:

public JsonResult Person()
{
    return Json(new CustomJsonViewResult
    {
        View = View(),
        Subject = "Update an existing person"
    }, JsonRequestBehavior.AllowGet);
}

There would be added BL logic to determine the subject, rather than the static value that's there now.

On the receiving client I have an AngularJS directive with a link like this:

link: function (scope, element, attrs) {
    screensService.GetPartialView(attrs.view).then(function (viewData) {

        var linkFunc = $compile(viewData.View);
        var content = linkFunc(scope);
        element.append(content);
    });
}

This works when I return the ActionResult directly, but in the current set up I get this error:

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)

If I log the viewData to console, I get this:

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

Summary

Any idea what the cause is of this Cannot read property 'ownerDocument' of undefined error? As far as I can see everything is available as expected.

Spikee
  • 3,967
  • 7
  • 35
  • 68
  • 1
    Refer [this answer](http://stackoverflow.com/questions/4730777/mvc-return-partial-view-as-json) for adding a view to a Jsonresult –  Aug 06 '15 at 07:28

1 Answers1

0

I got it to work using the Url approach:

public ActionResult Person()
{
    return View();
}

public JsonResult PersonAsJson()
{
    return Json(new CustomJsonViewResult
    {
        Url = Url.Action("Person"),
        Subject = "Update an existing person"
    }, JsonRequestBehavior.AllowGet);
}

And then on the client side:

screensService.GetPartialView(attrs.view).then(function (viewData) {
    $.post(viewData.Url, function (view) {
        var linkFunc = $compile(view);
        var content = linkFunc(scope);
        element.append(content);
    });
});

RenderRazorViewToString doesn't work because the compiler can't find ViewData.Model (nor does Resharper).

RenderView doesn't work because I can't provide a ViewResult into a ViewContext.

Thanks!

Spikee
  • 3,967
  • 7
  • 35
  • 68