Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 如何在使用MVC3的视图中某个位置的视图中插入局部视图?_Asp.net Mvc 3_Pjax - Fatal编程技术网

Asp.net mvc 3 如何在使用MVC3的视图中某个位置的视图中插入局部视图?

Asp.net mvc 3 如何在使用MVC3的视图中某个位置的视图中插入局部视图?,asp.net-mvc-3,pjax,Asp.net Mvc 3,Pjax,我有一个正在实现的MVC3应用程序。除了在服务器端加载一个在客户端还没有主视图的地址时该做什么之外,其他一切都正常工作。我的控制器代码看起来像 public virtual ActionResult Details(Guid id) { var partDetail = new PartDetail(id); var partialView = PartialView("Details", partDetail); if(Request.H

我有一个正在实现的MVC3应用程序。除了在服务器端加载一个在客户端还没有主视图的地址时该做什么之外,其他一切都正常工作。我的控制器代码看起来像

public virtual ActionResult Details(Guid id)
    {
        var partDetail = new PartDetail(id);
        var partialView = PartialView("Details", partDetail);
        if(Request.Headers["X-PJAX"]!= null)
        {
            return partialView;
        }

        var mainView =  View("Index");
        // Stick Partial View into main view at #update_panel?
        return mainView;
    }

如何将局部视图粘贴到主视图中,使其在“更新”面板中插入局部视图?

好的,如果不进行重大重构,您可以执行以下操作

(这假设您能够将index.cshtml上的
@model
设置为
PartDetail()

在上面的控制器操作中,更改:

var mainView =  View("Index");
致:

然后,在index.cshtml中添加以下内容:

<div id="update_panel">@RenderPartial("Details", Model)</div>
public class IndexViewModel
{
    ModelForIndex Index{get; set;}
    PartDetail Details{get; set;}
}
此重构的viewmodel将作为
@model IndexViewModel
添加到index.cshtml中,并由部分用户使用为:

<div id="update_panel">@RenderPartial("Details", Model.Details)</div>
@RenderPartial(“细节”,Model.Details)

希望这是有意义的。

好的,我知道你在那里做什么了……我使用索引视图作为其他几个部分的容器。我应该做个模特吗=空语句?这可能在没有重大重构的情况下工作,但我必须充实一下。你会建议什么样的“大”重构?见上面的修正案-可能会有小的调整(或不:-)我会在午饭后开始玩这个,但我认为这是一个相当不错的方式。
<div id="update_panel">@RenderPartial("Details", Model.Details)</div>