1

I am in C# MVC (.NET) and have a form where I have input with @html.TextAreaFor stored as a string I am assuming that the data from this text box will store as a string, but what I am wondering is how will the string look, in terms of "/n" elements, will the string delimit new line markers?

Ideally, I need to parse the textbox line-by-line, where the string on each line becomes an element of a List For example, if we have in the textbox

  item1
  item2
  item3

Then when I submit the form, the model that I specified will be loaded with the string form the textbox, and using that string we will load up our List (somewhere else, in a different model..)with item1, item2, and item3.

5
  • 1
    Use List<string> list = new List<string>(); string[] lines = textareamodel.Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries), then use foreach (string line in lines) { list.Add(line); }. I can write as answer if this trick works. Commented Jul 5, 2017 at 7:21
  • Okay I will try it and see Commented Jul 5, 2017 at 7:25
  • It could be done in a single line List<string> list = textareamodel.Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries).ToList() . Commented Jul 5, 2017 at 7:33
  • @mmushtaq I agree with you by using ToList() as List<string>, but let's see if OP solves his problem first. Commented Jul 5, 2017 at 7:35
  • Verified it to work, thanks Commented Jul 5, 2017 at 8:35

1 Answer 1

5

Suppose you have TextAreaFor bound to a model property named TextAreaModel which contains strings from user input like this:

@model ViewModel

@Html.TextAreaFor(m => m.TextAreaModel, new { ... })

The proper way to extract lines from model property is splitting the string content using Environment.NewLine as delimiter in POST action method as shown in example below:

[HttpPost]
public ActionResult SubmitForm(ViewModel model)
{
    // other stuff   

    // parse every textarea lines as list elements
    List<string> list = model.TextAreaModel.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();

    // assign it to other list property on other model
    OtherModel.OtherList = list;

    // other stuff

    return View(model);
}

NB: StringSplitOptions.RemoveEmptyEntries will remove empty lines found in source string, which I think this is more recommended way than using StringSplitOptions.None.

Demos:

.NET Fiddle Example - Splitting By Newline

.NET Fiddle Example - MVC Implementation

Related issue:

How to parse user input from a textarea line by line

Sign up to request clarification or add additional context in comments.

1 Comment

I had to make sure I was storing the property as a string and not a string[] or the Split method is not applicable. Once I changed that it worked perfectly.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.