【问题标题】:UIHint does not work with IList?UIHint 不适用于 IList?
【发布时间】:2011-10-26 20:15:29
【问题描述】:

我的 ViewMode 中有一个属性:

[UIHint("FileUpload")]
public IList<string> Images { get; set; }

在视图中Create.cshtml

@html.ValidationSummary(true)
@html.EditorForModel()

在文件夹Shared/EditorTemplates/FileUpload.cshtml

<h3>Test</h3>

但该字段不显示。简单地说,什么都没有发生!

我对另一种类型的字段进行了相同的测试,并且成功了:

[UIHint("FileUpload")]
public string Test { get; set; }

可能出了什么问题? 你如何解决这个问题?

如果我在 Create.cshtml 视图中手动添加以下代码,它就可以工作!

@Html.EditorFor(m => m.Images)

我不知道该怎么办。

【问题讨论】:

    标签: asp.net-mvc-3 razor data-annotations partial-views


    【解决方案1】:

    是的,UIHint 不适用于列表。您需要在相应的编辑器模板 (~/Views/Shared/EditorTemplates/FileUpload.cshtml) 中创建一个循环。 UIHint 模板传递了模型,在本例中为 IList&lt;string&gt;

    @model IList<string>
    @foreach (var item in Model)
    {
        ...
    }
    

    【讨论】:

    • 但是为什么如果我手动将代码@Html.EditorFor(m =&gt; m.Images) 放在视图上它可以工作!?我认为问题不在于UIHint 而在于@Html.EditorForModel(); 否则代码@Html.EditorFor(m =&gt; m.Images) 将不起作用
    • @RidermandeSousaBarbosa,如果你使用@Html.EditorForModel(),你可以为给定的视图模型(~/Views/Shared/EditorTemplates/MyViewModel.cshtml)定义一个自定义编辑器模板,你可以使用@Html.EditorFor(m =&gt; m.Images)。事实上,EditorForModel 可能不会递归到子编辑器模板中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    相关资源
    最近更新 更多