0

I am coming across an error, trying to do something similar to this

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

I can't do this. What is the correct way to make the list of strings output as desired.

UPDATE: Sorry, I am curious as to why I am unable to use a strongly typed version of an html helper, for joining a list of strings to a textbox value? For example, tags contains "mvc", "css" and "code" . I want the textbox to be prepopulated with the list as a list of strings.

2
  • Can you explain what exactly you are trying to do?
    – Kami
    Jan 7, 2016 at 23:46
  • What is your expected HTML output? You might be using the wrong helper.
    – Cᴏʀʏ
    Jan 7, 2016 at 23:49

2 Answers 2

1

I think you might be using the wrong helper -- the one you're trying to use is expecting an expression that represents a member of the model class, and also isn't meant to have a value given to it (it uses the property value). Try a different helper method, one that takes the element name, value, and html attributes:

@Html.TextArea("elementName", String.Join(", ", Model.Tags), new { @class = "form-control" })
2
  • This seemed to do the trick. What is the main difference between TextArea and TextAreaFor?
    – ddeamaral
    Jan 8, 2016 at 0:02
  • @destructi6n My understanding is that TextAreaFor will create textarea for all the Properties in an object that you specify in the expression. For example, if you have Employee object with Name, Address, Title properties and pass 'model => model.Employee' expression to the method, then it will create text area for Name, Address and Title
    – h-rai
    Jan 8, 2016 at 0:22
0

If you set your model.Tags property as a String. Split by comma in the controller.

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.
                }

On Preview/Edit Screen :

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.