2016-01-07 69 views
0

我来跨越一个错误,试图做类似这样的HTML辅助收集到字符串

 @Html.TextAreaFor(m => String.Join(",", Model.Tags), new { @class = "form-control" }) 

我不能做这个事情。根据需要制作字符串输出列表的正确方法是什么?

更新: 对不起,我很好奇,为什么我无法使用html助手的强类型版本,将字符串列表连接到文本框值?例如,标签包含“mvc”,“css”和“code”。我希望将文本框预先填充到列表中作为字符串列表。

+0

你能解释一下到底是什么你想干什么? – Kami

+0

您期望的HTML输出是什么?你可能会使用错误的帮手。 –

回答

1

我认为你可能会使用错误的助手 - 你正在尝试使用的是希望表达一个表达模型类的成员,也并不意味着它有一个值给它(它使用属性值)。尝试不同的辅助方法,即需要该元素的名称,价值,和HTML属性:

@Html.TextArea("elementName", String.Join(", ", Model.Tags), new { @class = "form-control" }) 
+0

这似乎是个窍门。 TextArea和TextAreaFor的主要区别是什么? – ddeamaral

+0

@ destructi6n我的理解是,TextAreaFor将为您在表达式中指定的对象中的所有属性创建textarea。例如,如果您的Employee对象具有Name,Address,Title属性并将'model => model.Employee'表达式传递给该方法,那么它将为该名称,地址和标题创建文本区域 –

0

如果您设置model.Tags属性为一个字符串。在控制器中用逗号分割。

if (!string.IsNullOrEmpty(model.Tags)) 
       { 

        string[] _tags={}; 
        if (model.Tags.Contains(",")) 
        { 
         _tags = model.Tags.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); 
        } 
        else _tags =new[]{ model.Tags}; 


        // Use _tags to save Db. 
       } 

在预览/编辑屏幕:

mymodel.Tags = string.Join(",",_data.Tags.Select(m => m.Tag)); 
return mymodel;