asp.net-mvc-3 - MVC 3 EditorFor 指定模板时的列表

标签 asp.net-mvc-3

我比赛迟到了。我正在与 EditorFor 斗争。

不是使用模板在文本框中显示实体的名称,而是将名称写入屏幕。它似乎正在写入对象的第一个属性,就像在我的真实项目中正在写入 guid 一样。

如何在指定模板时使用编辑器?

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

家居模型

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

房间模型

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)

当前输出:

enter image description here

最佳答案

您需要将编辑器模板放在名为 EditorTemplates 的目录中。所以正确的路径应该是这样的:

/Views/Home/EditorTemplates/_RoomModelEditor.cshtml

来自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.

关于asp.net-mvc-3 - MVC 3 EditorFor 指定模板时的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12789481/

相关文章:

c# - 如何重构此 C# 代码以使其更易于阅读?

asp.net-mvc - 防止asp.net mvc应用程序中的多个POST

asp.net-mvc-3 - ASP.NET MVC3 和 jquery.getJSON 不发出请求

c# - 在 asp.net mvc 3 和 jquery 中记录帖子

c# - 是否可以在 MVC 3 中打开/关闭验证数据注释?

c# - 为什么我的 Html.DropDownList 会生成一个充满 "System.Web.Mvc.SelectListItem"字符串的列表?

javascript - 更改禁用复选框的值

c# - 如何在发送到浏览器之前获取 HTML

c# - 如何从使用布局的 View 更改 html

asp.net-mvc-3 - Web 网格 MVC 3 - 用于检查条件的 If 语法