我如何能有一个视图呈现部分(用户控件)从不同的文件夹? 有了预览3,我习惯用完整的路径调用RenderUserControl,但升级到预览5,这是不可能的了。 相反,我们得到了RenderPartial方法,但它没有提供我所寻找的功能。
当前回答
你应该试试这个
~/Views/Shared/parts/UMFview.ascx
将~/Views/放在代码之前
其他回答
VirtualPathProviderViewEngine是WebFormsViewEngine的基础,它应该支持路径前面的“~”和“/”字符,所以上面的例子应该可以工作。
我注意到你的例子使用了路径“~/Account/myPartial”。ascx”,但是您提到您的用户控件在Views/Account文件夹中。你试过了吗
< %。RenderPartial(“~点击量/ Html / myPartial . ascx”);% >账户
还是你的问题打错了?
如果你经常使用其他路径,你可以永久地修复它,而不需要一直指定路径。默认情况下,它会检查“视图”文件夹和“共享”文件夹中的部分视图。但如果你想加一个。
在Models文件夹中添加一个类:
public class NewViewEngine : RazorViewEngine {
private static readonly string[] NEW_PARTIAL_VIEW_FORMATS = new[] {
"~/Views/Foo/{0}.cshtml",
"~/Views/Shared/Bar/{0}.cshtml"
};
public NewViewEngine() {
// Keep existing locations in sync
base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(NEW_PARTIAL_VIEW_FORMATS).ToArray();
}
}
然后在Global.asax.cs文件中添加以下一行:
ViewEngines.Engines.Add(new NewViewEngine());
我想出了一个变通办法,看起来效果不错。我发现需要切换到不同控制器的上下文进行动作名称查找、视图查找等。为了实现这一点,我为HtmlHelper创建了一个新的扩展方法:
public static IDisposable ControllerContextRegion(
this HtmlHelper html,
string controllerName)
{
return new ControllerContextRegion(html.ViewContext.RouteData, controllerName);
}
ControllerContextRegion定义为:
internal class ControllerContextRegion : IDisposable
{
private readonly RouteData routeData;
private readonly string previousControllerName;
public ControllerContextRegion(RouteData routeData, string controllerName)
{
this.routeData = routeData;
this.previousControllerName = routeData.GetRequiredString("controller");
this.SetControllerName(controllerName);
}
public void Dispose()
{
this.SetControllerName(this.previousControllerName);
}
private void SetControllerName(string controllerName)
{
this.routeData.Values["controller"] = controllerName;
}
}
在视图中使用的方式如下:
@using (Html.ControllerContextRegion("Foo")) {
// Html.Action, Html.Partial, etc. now looks things up as though
// FooController was our controller.
}
如果您的代码要求控制器路由组件保持不变,那么这种方法可能会产生不必要的副作用,但在我们的代码中,到目前为止,这种方法似乎没有任何负面影响。
只需要包含视图的路径和文件扩展名。
剃须刀:
@Html.Partial("~/Views/AnotherFolder/Messages.cshtml", ViewData.Model.Successes)
ASP。NET引擎:
<% Html.RenderPartial("~/Views/AnotherFolder/Messages.ascx", ViewData.Model.Successes); %>
如果这不是你的问题,你能包括你的代码,用来与RenderUserControl工作吗?
尝试使用RenderAction("myPartial","Account");
推荐文章
- asp.net MVC中的@RenderSection是什么
- 如何在c#中获得正确的时间戳
- Linq选择列表中存在的对象(A,B,C)
- c# .NET中的App.config是什么?如何使用它?
- 在哪里放置AutoMapper.CreateMaps?
- c#:如何获得一个字符串的第一个字符?
- String类中的什么方法只返回前N个字符?
- 我如何提高ASP。NET MVC应用程序性能?
- 更好的方法将对象转换为int类型
- 我可以将c#字符串值转换为转义字符串文字吗?
- 在c#中转换char到int
- c#中朋友的对等物是什么?
- 关键字使用virtual+override vs. new
- 在ASP中选择Tag Helper。NET Core MVC
- 如何在没有任何错误或警告的情况下找到构建失败的原因