EditorTemplate。 MVC3

时间:2012-01-07 13:58:05

标签: asp.net-mvc-3 razor mvc-editor-templates

我创建了EditorTemplate:

enter image description here

使用代码:

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

但我在页面上看不到任何编辑器......为什么?

1 个答案:

答案 0 :(得分:2)

您的编辑器模板模型显示其类型为PhonesViewModel,但您使用model.Phoness调用它IEnumerable of PhoneViewModel

在您的编辑模板文件中,更改@model RentSite.Web.UI.Models.PhonesViewModel

@model IEnumerable<RentSite.Web.UI.Models.PhonesViewModel>