尝试在ASP.NET MVC3视图中使用带有ICollection 的EditorFor?
我正在尝试在Create
视图中显示一个类对象,其中一个属性是ICollection
。
例如…
namespace StackOverflow.Entities { public class Question { public int Id { get; set; } .... public ICollection Tags { get; set; } } }
如果视图就像StackOverflow’问一个问题’页面,那么Tags
html元素是一个单独的input box
..我不知道如何在ASP.NET MVC3视图中做到这一点?
有任何想法吗?
我尝试使用EditorFor
但浏览器中没有显示任何内容,因为它不确定如何呈现字符串集合。
首先使用[UIHint]
属性修饰视图模型:
public class Question { public int Id { get; set; } [UIHint("tags")] public ICollection Tags { get; set; } }
然后在主视图中:
@model StackOverflow.Entities.Question @Html.EditorFor(x => x.Tags)
然后你可以编写一个自定义编辑器模板( ~/Views/Shared/EditorTemplates/tags.cshtml
):
@model ICollection @Html.TextBox("", string.Join(",", Model))
或者如果您不喜欢装饰,您还可以直接在视图中指定要用于给定属性的编辑器模板:
上述就是C#学习教程:尝试在ASP.NET MVC3视图中使用带有ICollection 的EditorFor?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
@model StackOverflow.Entities.Question @Html.EditorFor(x => x.Tags, "tags")
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/987531.html