如何返回由控制器另一个控制器的局部视图? [英] How to Return partial view of another controller by controller?

查看:461
本文介绍了如何返回由控制器另一个控制器的局部视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XXX.cshtml 文件视图\\ ABC 文件夹中。它的控制器是 ABC

I have an XXX.cshtml file in a Views\ABC folder. Its controller is ABC

我也有我的 DEF 控制器的操作方法返回一个 Partialview(XXX,xyzmodel)

I also have an action method in my DEF controller that return a Partialview("XXX" , xyzmodel)

我得到一个视图未找到错误。

I get a "view not found" error.

如何调用的视图的其他控制器

How to call that view from other controller

推荐答案

通常的看法是属于一个特定的配套控制器的支持其数据要求,或视图中的查看/共享文件夹,如果控制器之间共享(因此得名)。

Normally the views belong with a specific matching controller that supports its data requirements, or the view belongs in the Views/Shared folder if shared between controllers (hence the name).

您的可以的参考意见/局部视图从另一个控制器,通过指定的完整路径(包括扩展名),如:

You can refer to views/partial views from another controller, by specifying the full path (including extension) like:

return PartialView("~/views/ABC/XXX.cshtml", zyxmodel);

@Max托罗的相对路径(没有扩展名)的基础上,答案

return PartialView("../ABC/XXX", zyxmodel);

,但是这个却ANYWAY是不是一个好主意

*注:这些是的只有两个语法工作的。没有 ABC \\\\ XXX ABC / XXX 或任何其他变因为这些都是相对的路径,并没有找到匹配。

*Note: These are the only two syntax that work. not ABC\\XXX or ABC/XXX or any other variation as those are all relative paths and do not find a match.

您可以使用 Html.Renderpartial 在你看来不是,但它需要扩展,以及:

You can use Html.Renderpartial in your view instead, but it requires the extension as well:

Html.RenderPartial("~/Views/ControllerName/ViewName.cshtml", modeldata);

使用 @ Html.Partial 内联剃刀语法:

@Html.Partial("~/Views/ControllerName/ViewName.cshtml", modeldata)

您可以使用 ../控制器/视图语法不带扩展名(再次信贷@Max托罗):

You can use the ../controller/view syntax with no extension (again credit to @Max Toro):

@Html.Partial("../ControllerName/ViewName", modeldata)

注:显然的RenderPartial 略比偏快,但是这并不重要。

Note: Apparently RenderPartial is slightly faster than Partial, but that is not important.

如果你想实际调用另一个控制器,使用方法:

If you want to actually call the other controller, use:

@Html.Action("action", "controller", parameters)

推荐的解决方案:@ Html.Action

我个人的preference是使用 @ Html.Action ,因为它允许每个控制器来管理自己的看法,而不是相互参照的观点与其他控制器(这导致了大量意大利面条般混乱)。

Recommended solution: @Html.Action

My personal preference is to use @Html.Action as it allows each controller to manage its own views, rather than cross-referencing views from other controllers (which leads to a large spaghetti-like mess).

您通常会通过刚才所需要的密钥值(像任何其他视图)如您例如:

You would normally pass just the required key values (like any other view) e.g. for your example:

@Html.Action("XXX", "ABC", new {id = model.xyzId })

这将执行 ABC.XXX 动作和渲染的结果就地。这使得视图和控制器保持独立自足(即可重复使用)。

This will execute the ABC.XXX action and render the result in-place. This allows the views and controllers to remain separately self-contained (i.e. reusable).

我刚刚打了一个情况,我不能使用@ Html.Action,而是要创建一个基于动作视图路径所需控制器名称。为此我加入这个简单的查看扩展方法 UrlHelper 因此可以说回报 PartialView (Url.View(actionName,controllerName),modelData)

I have just hit a situation where I could not use @Html.Action, but needed to create a view path based on a action and controller names. To that end I added this simple View extension method to UrlHelper so you can say return PartialView(Url.View("actionName", "controllerName"), modelData):

public static class UrlHelperExtension
{
    /// <summary>
    /// Return a view path based on an action name and controller name
    /// </summary>
    /// <param name="url">Context for extension method</param>
    /// <param name="action">Action name</param>
    /// <param name="controller">Controller name</param>
    /// <returns>A string in the form "~/views/{controller}/{action}.cshtml</returns>
    public static string View(this UrlHelper url, string action, string controller)
    {
        return string.Format("~/Views/{1}/{0}.cshtml", action, controller);
    }
}

这篇关于如何返回由控制器另一个控制器的局部视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆