c# - 尝试在 ASP.NET MVC3 View 中将 EditorFor 与 ICollection<string> 一起使用?

标签 c# .net asp.net-mvc-3 editorfor

我试图在 Create 中显示一个类对象 View ,其中属性是 ICollection<string> .

例如……

namespace StackOverflow.Entities
{
    public class Question
    {
        public int Id { get; set; }
        ....
        public ICollection<string> Tags { get; set; }
    }
}

如果 View 类似于 StackOverflow 的“提问”页面,其中 Tags html 元素是单个 input box .. 我不确定如何在 ASP.NET MVC3 View 中做到这一点?

有什么想法吗?

我尝试使用 EditorFor但浏览器中什么也没有显示,因为它不确定如何呈现字符串集合。

最佳答案

首先用 [UIHint] 装饰你的 View 模型属性:

public class Question
{
    public int Id { get; set; }

    [UIHint("tags")]
    public ICollection<string> Tags { get; set; }
}

然后在主视图中:

@model StackOverflow.Entities.Question
@Html.EditorFor(x => x.Tags)

然后您可以编写自定义编辑器模板 (~/Views/Shared/EditorTemplates/tags.cshtml):

@model ICollection<string>
@Html.TextBox("", string.Join(",", Model))

或者如果你不喜欢装饰,你也可以直接在 View 中指定要用于给定属性的编辑器模板:

@model StackOverflow.Entities.Question
@Html.EditorFor(x => x.Tags, "tags")

关于c# - 尝试在 ASP.NET MVC3 View 中将 EditorFor 与 ICollection<string> 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7635555/

相关文章:

c# - WCF 与托管应用程序通信?

c# - 从后面的代码中的列表框中获取 SelectedItem

c# - 索引和长度必须引用子字符串中字符串错误中的位置

.net - Spring.NET 使用多个 PropertyPlaceholderConfigurers 不起作用

mysql - MVC3 只读 MySql 连接字符串

c# - 如何在目标线程上运行事件处理程序

C#:为什么 x 的最终值与 for 循环外的值不同?

c# - 在 .NET Windows 窗体中,如何在两个 EXE 或应用程序之间发送数据?

asp.net - html.ListBoxFor报错问题asp.mvc 3

javascript - ajax调用后更新div