2

I am late to the game. i am struggling with EditorFor.

Instead of using the template to display the name of the entity in a textbox, the name is written to screen. It appears to be writing the first property of the object as in my real project a guid is being written.

How can I use the editorfor while specifying the template?

Home Controller

public ActionResult Index()
    {
            HomeModel homeModel = new HomeModel();
            homeModel.RoomModels = new List<RoomModel>();
            homeModel.RoomModels.Add(new RoomModel() { RoomName = "Room-1" });
            homeModel.RoomModels.Add(new RoomModel() { RoomName = "Room-2" });
            homeModel.RoomModels.Add(new RoomModel() { RoomName = "Room-3" });

            return View(homeModel);
    }

Home Model

public class HomeModel
    {
        public List<RoomModel> RoomModels { get; set; }
    }

Room Model

public class RoomModel
{
    public string RoomName { get; set; }
}

/Views/Home/Index.cshtml

@model MVC.Temp.Models.Home.HomeModel

<h1>Home</h1>
<div id="RoomModels">
    @Html.EditorFor(m => m.RoomModels, "_RoomModelEditor")
</div>

/Views/Home/_RoomModelEditor.cshtml

@model MVC.Temp.Models.Home.RoomModel
@Html.TextBoxFor(x => x.RoomName)

Current Output:

enter image description here

3 Answers 3

4

You need to put your editor template in directory called EditorTemplates. So the correct path should be the following:

/Views/Home/EditorTemplates/_RoomModelEditor.cshtml

From the documentation:

If a template whose name matches the templateName parameter is found in the controller's EditorTemplates folder, that template is used to render the expression. If a template is not found in the controller's EditorTemplates folder, the Views\Shared\EditorTemplates folder is searched for a template that matches the name of the templateName parameter. If no template is found, the default template is used.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there any way to specify a full path? I have two areas and one area needs to use the other areas editortemplate. So far I have tried to the overload specifying the full path on the EditorFor method. (usually this works with html.renderpartial). "~/Areas/Area1/Views/Other/EditorTemplates/EditModel.cshtml"
If you want to use the same editortemplate from multiple contollers/areas you have to put in the /Views/Shared/EditorTemplates as the documentation states also in my answer. The framework first look in the controllers EditorTemplates and then in the Views\Shared\EditorTemplates and that is it. There is no way specify absolute paths for editor templates.
1

Create a folder under /Views/Home called EditorTemplates. Call your template RoomModel.cshtml.

So the full template name is /Views/Home/EditorTemplates/RoomModel.cshtml.

Using the name of the class for the name of the editor template means that whenever you do @Html.EditorFor(m => m.RoomModels) it will use your editor template automatically.

Update

If you have multiple controllers with views that need to shared the same editor template then put your template in /Views/Shared/EditorTemplates.

2 Comments

@Valamas - except that I just reread your comment and noticed the use of Areas. I haven't used areas before, so I don't know if it's possible to share templates like this.
thanks. I guess i will have to work with that. I am not a fan of the "shared" views unless they are truly shared. Like layouts. Otherwise I always assign a view to live somewhere.
-1

I think your problem is that you are passing a List<RoomModel> instead of a RoomModel.

You should change your EditorTemplate to accept a List... or do a Foreach in your view displaying each RoomModel.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.