我创建了EditorTemplate:
使用代码:
@model RentSite.Web.UI.Models.PhonesViewModel
<div>
@Html.TextBoxFor(m=>Model.Number) @Html.TextBoxFor(m => Model.From) @Html.TextBoxFor(m => Model.To)
</div>
我试着像这样使用它:
@model RentSite.Web.UI.Models.ContactsViewModel
@{
ViewBag.Title = "AddContact";
}
<h2>AddContact</h2>
@using (Html.BeginForm("AddContact","Contact",FormMethod.Post, new {enctype = "multipart/form-data"}))
{
<input type="file" name="Image"/>
@Html.EditorFor(model=>model.Phoness )
<input type="submit" value="Add"/>
}
ContactsViewModel如下所示:
namespace RentSite.Web.UI.Models
{
public class ContactsViewModel
{
public string Name { get; set; }
public IEnumerable<PhonesViewModel> Phoness { get; set; }
}
}
但我在页面上看不到任何编辑器......为什么?
答案 0 :(得分:2)
您的编辑器模板模型显示其类型为PhonesViewModel,但您使用model.Phoness调用它IEnumerable of PhoneViewModel
。
在您的编辑模板文件中,更改@model RentSite.Web.UI.Models.PhonesViewModel
到
@model IEnumerable<RentSite.Web.UI.Models.PhonesViewModel>