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

时间:2014-02-02 20:01:31

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-partialview

在我的mvc项目中,我有一个控制器,其中包含以下操作:

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

相应的索引视图使用主网格呈现局部视图:

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

此局部视图有一个提交按钮和一个网格。客户需要在表单中放入一些数据并将其提交给该操作:

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

但是我得到没有布局的网格。如何使用布局返回局部视图?

1 个答案:

答案 0 :(得分:1)

您应该明确返回正确的视图:

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

这确保了当您对此操作发布帖子时,它会返回索引视图。我在这里假设repository.Compare也返回List<Product>,因为类型需要匹配。