【问题标题】:Reusing MVC PartialView editor/display templates for action PartialViewResult为操作 PartialViewResult 重用 MVC PartialView 编辑器/显示模板
【发布时间】:2012-02-16 11:01:12
【问题描述】:

我在Views/DisplayTemplates 中有一个名为Bar.cshtml 的局部视图显示模板,因此它可以像这样使用:

[DataType("Bar")]  
public FooBar Foo {get;set;}

不幸的是,如果我想在return PartialView("Bar",fooModel); 之类的操作中使用它,它将找不到它,因为它不在搜索的文件夹列表中。目前我已经制作了该文件的副本并将其放在Views/DisplayTemplatesViews 中,但是有没有正确的方法来使用一个文件呢?

【问题讨论】:

    标签: templates model-view-controller action reusability asp.net-mvc-partialview


    【解决方案1】:

    希望我现在通过提供一个继承自 razor 视图引擎的自定义视图引擎并简单地添加要搜索的视图位置来完成它:

    using System.Linq;
    using System.Web.Mvc;
    
    namespace MvcApplication1
    {
    public class CustomViewEngine : RazorViewEngine
    {
        public CustomViewEngine()
            : this(null)
        {
    
        }
    
        public CustomViewEngine(IViewPageActivator activator)
            : base(activator)
        {
            var partialViewLocationFormatsList = PartialViewLocationFormats.ToList();
    
            partialViewLocationFormatsList.Add("~/Views/{1}/DisplayTemplates/{0}.cshtml");
            partialViewLocationFormatsList.Add("~/Views/{1}/EditorTemplates/{0}.cshtml");
            partialViewLocationFormatsList.Add("~/Views/Shared/DisplayTemplates/{0}.cshtml");
            partialViewLocationFormatsList.Add("~/Views/Shared/EditorTemplates/{0}.cshtml");
    
            PartialViewLocationFormats = partialViewLocationFormatsList.ToArray();
    
            var areaPartialViewLocationFormatsList = AreaPartialViewLocationFormats.ToList();
    
            areaPartialViewLocationFormatsList.Add("~/Areas/{2}/Views/{1}/DisplayTemplates/{0}.cshtml");
            areaPartialViewLocationFormatsList.Add("~/Areas/{2}/Views/{1}/EditorTemplates/{0}.cshtml");
            areaPartialViewLocationFormatsList.Add("~/Areas/{2}/Views/Shared/DisplayTemplates/{0}.cshtml");
            areaPartialViewLocationFormatsList.Add("~/Areas/{2}/Views/Shared/EditorTemplates/{0}.cshtml");
    
            AreaPartialViewLocationFormats = areaPartialViewLocationFormatsList.ToArray();
        }
    }
    }
    

    然后在 Global.asax 中注册:

    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new CustomViewEngine());     
    

    【讨论】:

      【解决方案2】:

      如果您打算在多个页面中使用它,您应该在Views/Shared/DisplayTemplates 中创建一个文件夹。要将此模板用于您的 FooBar Foo 属性,请使用 [UIHint("Bar")] 属性对其进行装饰。

      【讨论】:

      • 这无济于事,因为 UIHint 与 DataType 做同样的事情。异常表明它没有被寻找: InnerException: System.InvalidOperationException Message=未找到局部视图“Bar”或没有视图引擎支持搜索的位置。搜索了以下位置:~/Views/Home/Bar.aspx ~/Views/Home/Bar.ascx ~/Views/Shared/Bar.aspx ~/Views/Shared/Bar.ascx ~/Views/Home/Bar。 cshtml ~/Views/Home/Bar.vbhtml ~/Views/Shared/Bar.cshtml ~/Views/Shared/Bar.vbhtml
      • 您的 Bar.cshtml 是在 Views/DisplayTemplates 中还是在 Views/Shared/DisplayTemplates 或 Views/Home/DisplayTemplates 中?您指定的那个没有被搜索。如果您将其保留在 Views/DisplayTemplates 中,请尝试将其移至 /Views/Shared/DisplayTemplates 或 /Views/Home/DisplayTemplates
      • 它在 Views/Shared/DisplayTemplates 中,虽然我把它放在哪里并不重要,因为 MVC 不会在 /DisplayTemplates 中搜索,这就是问题所在
      猜你喜欢
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多