如何在MVC中返回多个视图

时间:2015-12-16 05:51:28

标签: asp.net-mvc partial-views

我只是想知道我是否有一个有两个部分视图的视图。单个控制器如何返回多个视图?

e.g。 我有一个带搜索按钮的搜索页面。 您必须选择按组织或服务进行搜索。 我创建了两个部分视图来显示结果,因为它们在化妆上有所不同。

这些部分视图显示在主搜索视图中。

Html.Partial("_OrgResult") // partialview of results 
Html.Partial("_ServiceResult") // partialview of results

我有一个像这样的控制器

    [HttpPost]
    public ActionResult GetResults(int SearchType = 0, string SeartchTxt = "")
    {

        if (SearchType < 2)
        {
            return PartialView("~/Views/Search/_OrgResult.cshtml", GetOrganisationResults(SearchType, SeartchTxt));
        }
        else
        {
            return PartialView("~/Views/Search/_ServicehResult.cshtml", GetServiceResults(SearchType, SeartchTxt));
        }

    }

这是正确的方法吗? 它会工作吗?

1 个答案:

答案 0 :(得分:0)

最好的方法是在视图中添加一条HTML.partial行:

Html.Partial("Result") // partialview of results 

然后,在控制器中,返回与参数对应的视图:

[HttpGet]
public ActionResult Results(int SearchType = 0, string SeartchTxt = "")
{

    if (SearchType < 2)
    {
        return PartialView("~/Views/Search/_OrgResult.cshtml", GetOrganisationResults(SearchType, SeartchTxt));
    }
    else
    {
        return PartialView("~/Views/Search/_ServicehResult.cshtml", GetServiceResults(SearchType, SeartchTxt));
    }

}