Put editor templates in view's folder with ASP.NET Core MVC 6
Asked Answered
D

1

13

with MVC 4 I was able to put a view's editor templates into the view's folder: AwesomeApp/Views/UserMgmt/EditorTemplates/UserSettings.cshtml.

Now I am using ASP.NET Core MVC 6 and it does not find the editor template. I have to put them into AwesomeApp/Views/Shared/EditorTemplates/UserSettings.cshtml. What needs to be configured so I do not have to put all of my editor templates in this one folder?

I am using the latest version of Telerik's Kendo UI for ASP.NET MVC. But I guess it's something in the application itself.

Best regards, Carsten

Dimphia answered 28/7, 2016 at 14:28 Comment(3)
have you tried to change this to ViewComponent?Stringency
We switched back to MVC 5 for various reasons. So I am not able to check this anymore.Dimphia
Using the IViewLocationExpander interface should be able to do the trick. An example is available herePsychographer
S
3

This works out of the box in (at least) core 2.2. In one of the demos they have put the EditorTemplates folder underneath a views folder, not the shared folder.

I tested this myself using the following code...

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
    });
}

Home/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>

Home/Index.cshtml

@model TestEditorForModel

@Html.EditorFor(m => m)

Output

enter image description here

Scopas answered 20/8, 2019 at 13:28 Comment(1)
Good to know! I cannot test it anymore, but I trust that your screenshot is real. ;)Dimphia

© 2022 - 2024 — McMap. All rights reserved.