【问题标题】:What is the best way to render PartialView to a Layout Page in MVC?在 MVC 中将部分视图呈现到布局页面的最佳方法是什么?
【发布时间】:2016-08-16 07:15:46
【问题描述】:

我的MVC5项目中有一个布局页面,我想通过单击左侧的菜单链接(超链接)来呈现所有页面内容(部分视图),如下所示。有一些方法使用Ajax 等,但我不确定哪种方法最能满足我的需求。有没有关于这个问题的例子,包含Layout 页面和Controller 方法?

【问题讨论】:

    标签: javascript jquery asp.net-mvc razor asp.net-mvc-partialview


    【解决方案1】:

    您必须首先将您的正文内容包装在 div 中并为其分配任何 id:

    <div id="divContentArea">
    @RenderBody()
    </div>
    

    现在在脚本菜单点击事件

    $(".menuLinkItem").click(function (e) {
        e.preventDefault();        
        var controllerName = $(this).attr('data-controllername');
        var actionName = $(this).attr('data-actionname'); 
    
        if (String(actionName).trim() == '') {
            return false;
        }
        if (typeof (controllerName) == "undefined") {
            return false;
        }
    
        var url = "/" + controllerName + "/" + actionName;
    
        //Open url in new tab with ctrl key press
        if (e.ctrlKey) {
            window.open(url, '_blank');
            event.stopPropagation();
            return false;
        }            
    
        $.ajax({
            url: url,
            type: 'POST',
            success: function (data) {
                var requestedUrl = String(this.url).replace(/[&?]X-Requested-With=XMLHttpRequest/i, "");
                if (typeof (requestedUrl) == "undefined" || requestedUrl == 'undefined') {
                    requestedUrl = window.location.href;
                }
    
                // if the url is the same, replace the state
                if (typeof (history.pushState) != "undefined") {
                    if (window.location.href == requestedUrl) {
                        history.replaceState({ html: '' }, document.title, requestedUrl);
                    }
                    else {
                        history.pushState({ html: '' }, document.title, requestedUrl);
                    }
                }
    
                $("#divContentArea").html(data);                
    
            },
            error: function (xhr) {
            }
        });
    
    });
    

    控制器:

    [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
    public PartialViewResult Index()
    {
            if (HttpContext.Request.HttpMethod == "GET")
            {
                return View();
            }
            else
            {
                return PartialView();
            }
    }
    

    【讨论】:

    • 感谢您的回复。有一个小问题。尝试使用 Firebug 传递 success: function (data) { 行后,出现错误 ReferenceError: data is not defined。我还尝试定义一个数据参数并分配一个整数值进行测试,但没有任何意义。有什么想法吗?
    • 本例中可能有没有像data这样定义的参数吗?
    • 好的,我意识到我需要在我的 Controller 方法中使用 [AllowAnonymous]。现在它就像一个魅力:) 非常感谢您的回答...
    • 渲染局部视图后,我看到单击浏览器的刷新按钮会导致“NetworkError: 404 Not Found - localhost:43239/Account/_Register”。任何想法?如何让刷新按钮转发索引页面?
    • 我已经更新了我的答案,请检查一下:控制器动作调用更改
    猜你喜欢
    • 2012-01-01
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多