我比赛迟到了。我正在为 EditorFor 苦苦挣扎。
不是使用模板在文本框中显示实体的名称,而是将名称写入屏幕。它似乎正在编写对象的第一个属性,因为在我的真实项目中正在编写一个 guid。
如何在指定模板时使用 editorfor?
家庭控制器
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)
电流输出: