首页 > 解决方案 > ASP.NET Core MVC - 在 RenderBody() 中渲染视图

问题描述

_Layout.cshtml包含RenderBody()从 处的视图生成内容Index.cshtml

Index.cshtml有一个用 jQuery 加载 jstree 的大型脚本。

$('#tree').jstree({
        "plugins": ["search", "wholerow", "dnd", "contextmenu", "massload", "search", "types"],
        'core': {
            "check_callback": true, // enable all modifications, tweak for rules.
            'data': {
                'url': function (node) {
                    return node.id === '#' ? "/Home/GetRoot" : "/Home/GetChildren/" + node.id;
                },
                'data': function (node) {
                    return { 'id': node.id };
                }
            }
        }
    });

on 事件changed.jstree捕获有关所选节点的信息。我想在树下呈现一个视图,其中包含一个可编辑的表单,允许在树上执行 CRUD 操作。

$('#tree').on("changed.jstree", function (e, data) {
    $.ajax({
            type: "GET",
            url: "/Home/GetNodeInformation/" + data.selected,
            data: '{id: "' + data.selected + '" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                if (response['node_type'] == 'Portfolio') {
                    $('#nodeinfo').load('@Url.Action("GetPortfolioDetails", "Portfolio")');
                }
                console.log(response['node_type'])
            },
            failure: function (response) {
                alert(response.responseText);
            },
            error: function (response) {
                alert(response.responseText);
            }
        });
    });

渲染树和它上面的事件运行良好,我相信这是因为它RenderBody正在正常运行。

但我打电话的那一刻

$('#nodeinfo').load('@Url.Action("GetPortfolioDetails", "Portfolio")');

整个页面在该 div 中再次呈现。

问题 - 由于我的应用程序是单页应用程序,如何正确管理布局文件中的占位符以处理不同的渲染部分?在这种情况下,我看不到如何渲染树和它下面的详细信息窗格,因为它们是相互关联的。我不认为我可以在这种情况下使用 RenderBody,并且使用 jQuery 动态生成表单将很难维护。

标签: c#asp.net-core-mvc

解决方案


推荐阅读