2012-01-07 46 views
0

我創建EditorTemplate:EditorTemplate。 MVC3

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

但我無法在頁面上看到任何編輯器...爲什麼?

回答

2

您的編輯模板模型表示它的類型爲PhonesViewModel,但您使用model.Phoness調用它,即IEnumerable of PhoneViewModel

在編輯器中的模板文件,更改@model RentSite.Web.UI.Models.PhonesViewModel

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

它沒有我需要的到底是什麼。最後,它不起作用。 – korovaisdead 2012-01-07 14:16:16

+0

您的預期成果是什麼? – Shyju 2012-01-07 14:17:27

+0

我希望能夠通過此ContactsViewModel添加一個或多個電話。 – korovaisdead 2012-01-07 14:45:14