简体   繁体   English

如何正确加载MVC中的局部视图?

[英]How to load correctly a partial view in mvc?

In my mvc project i have a controller with the following actions: 在我的mvc项目中,我有一个具有以下操作的控制器:

public ActionResult Index()
    {
        return View(new List<Product>());
    }

The corresponding index view render a partial view with the master grid: 相应的索引视图使用主网格呈现局部视图:

@model System.Collections.Generic.List<TbbModels.Domain.Product>
@Html.Partial("_ProdutoMasterGrid", Model)

This partial view have a submit button and a grid. 此局部视图具有一个提交按钮和一个网格。 The client needs to put some data in the form and submit it to that action: 客户需要在表单中放入一些数据并将其提交给该操作:

 public ActionResult _ProdutoMasterGrid(string param)
    {
        return PartialView("_ProdutoMasterGrid",
        repository.Compare(param).ToList());
    }

But than i get the grid without the layout. 但是比起我没有布局的网格。 How can i return a partial view with the layout? 如何返回布局的局部视图?

You should explicitly return the correct view: 您应该显式返回正确的视图:

public ActionResult _ProdutoMasterGrid(string param)
{
    return View("Index",
    repository.Compare(param).ToList());
}

This ensures that when you do a post to this action, it returns the index view. 这样可以确保在您对此操作进行发布时,它返回索引视图。 I'm supposing here that repository.Compare return a List<Product> as well, since the type needs to match. 我在这里假设repository.Compare返回一个List<Product> ,因为类型需要匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM