【问题标题】:EditorTemplate. MVC3编辑器模板。 MVC3
【发布时间】:2012-01-07 13:58:05
【问题描述】:

我创建了 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; }
    }
}

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 这不是我所需要的。最后,它不起作用。
    • 你的预期结果是什么?
    • 我希望能够通过这个 ContactsViewModel 添加一部或多部手机。
    • 根据您的代码,此编辑器模板将为您的 IEnumerbale 中的每个条目显示 3 个文本框(数字、从和到)。您的问题是内容未显示。你确定你在 model.Phoness 中有物品吗?在您的控制器操作中放置断点并在将其传递给视图之前查看它。
    • 我已经明白我的错误了。我正在尝试添加新手机,但不显示。你能给我任何建议,我该如何组织这个?如何添加一部或多部手机?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多