【问题标题】:Put editor templates in view's folder with ASP.NET Core MVC 6使用 ASP.NET Core MVC 6 将编辑器模板放入视图的文件夹中
【发布时间】:2016-12-03 00:28:34
【问题描述】:

使用 MVC 4,我能够将视图的编辑器模板放入视图的文件夹:AwesomeApp/Views/UserMgmt/EditorTemplates/UserSettings.cshtml

现在我使用的是 ASP.NET Core MVC 6,但它没有找到编辑器模板。我必须将它们放入AwesomeApp/Views/Shared/EditorTemplates/UserSettings.cshtml。需要进行什么配置,这样我就不必将所有编辑器模板都放在这个文件夹中?

我正在为 ASP.NET MVC 使用 Telerik 的 Kendo UI 的最新版本。但我猜这是应用程序本身的问题。

最好的问候, 卡斯滕

【问题讨论】:

  • 你试过把它改成 ViewComponent 吗?
  • 出于各种原因,我们切换回了 MVC 5。所以我不能再检查这个了。
  • 使用IViewLocationExpander 接口应该可以解决问题。一个例子是可用的here

标签: asp.net-core-mvc kendo-asp.net-mvc mvc-editor-templates


【解决方案1】:

这在(至少)核心 2.2 中开箱即用。在one of the demos 中,他们将EditorTemplates 文件夹放在了views 文件夹下,而不是共享文件夹下。

我使用以下代码自己测试了这个...

public class TestEditorForModel
{
    [Display(Name = "Testing a string property")]
    public string StringProp { get; set; }

    [Display(Name = "Testing an integer property")]
    [Range(100, 1000)]
    public int IntProp { get; set; }

    [Display(Name = "Testing a decimal property")]
    public decimal DecProp { get; set; }
}

HomeController.cs

public IActionResult Index()
{
    return View(new TestEditorForModel
    {
        StringProp = "testing editorfor",
        IntProp = 123,
        DecProp = 123.456m
    });
}

主页/EditorTemplates/TestEditorForModel.cshtml

@model TestEditorForModel

<div>
    <label asp-for="StringProp"></label>
    <input asp-for="StringProp" placeholder="Testing" />
</div>

<div>
    <label asp-for="IntProp"></label>
    <input asp-for="IntProp" placeholder="123" />
</div>

<div>
    <label asp-for="DecProp"></label>
    <input asp-for="DecProp" placeholder="100.00" />
</div>

首页/Index.cshtml

@model TestEditorForModel

@Html.EditorFor(m => m)

输出

【讨论】:

  • 很高兴知道!我不能再测试它了,但我相信你的截图是真实的。 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
  • 2023-03-13
相关资源
最近更新 更多