<p>
<label for="Tags">Tags:</label>
<% String tagsText = "";
foreach (Tag item in Model.Tags)
{
tagsText += item.Name + " ";
}
%>
<%= Html.TextBox("Tags", tagsText.Trim()) %>
<%= Html.ValidationMessage("Tags", "*") %>
</p>
显然这段代码并不完美,我承认这一点。但是你会如何改进它呢?这对我来说似乎有点草率。
最佳答案
不是很干净,但这有一个额外的好处,即不在字符串末尾添加尾随空格。
<p>
<label for="Tags">Tags:</label>
<% string tagsText = string.join(" ", (from t in Model.Tags
select t.Name).ToArray<string>()); %>
<%= Html.TextBox("Tags", tagsText) %>
<%= Html.ValidationMessage("Tags", "*") %>
</p>
关于c# - 使用转换为字符串的数据列表填充 Html.TextBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2181990/