2

i want to make basic form with one textarea. I want to pass message from textarea to MVC Controller.

This is my method in controller:

    [HttpPost]
    public ActionResult FromInput(string[] input)
    {
        var c = input.ToString();
        return View("Succes");
    }

And my Index.cshtml:

<div class="row">
<form id="form" action="/Home/FromInput" method="post">
    <textarea name="input"> </textarea>
    <br />
    <input type="submit" id="submit_button" value="Submit" />
</form>

for now, my input variable in controller passed to function is string[1] array. In text area, i will have multiple line of texts. My question is, how to set every line of text from textarea to another place in input array? Is it possible?

For now, input var looks like this:

"One\r\nTwo\r\nThree"

Is it possible to put any line of text to another place in string array?

3 Answers 3

2

I faced a similar problem, this is how I dealt with it:

var splitInput = input.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

This creates an array "splitInput" from your "input" string, splitting on a newline.

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

1 Comment

This might not always work, there's no guarantee that a browser running on Windows, MacOS, Android, iOS, Linux, or any other OS would use the same convention for line endings as the server. Instead, enumerate the common possible ways that could be used.
2

input should be of type string not string[].

To transform in to an array you'll have to split the string on new lines;

input.Split(new[] { "\r\n", "\r", "\n", "\n\r"}, StringSplitOptions.None);

N.B. I wouldn't rely on Environment.NewLine as you can't be sure that the browser will submit newlines using the same standard as your server environment.

Comments

1

You need to pass the values from your View as one array and then you can receive them and play with them to turn them into whatever data structure you want.

using this code you can split the array while keeping in view the new lines and empty entries:

  var list = input[1].Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();

EDIT

as @phuzi suggested in comments that this might not work on all operating systems. so I looked that up and found he was right so here's some good information.

official MSDN says:

A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.

different operating systems have different escaping characters like \r\n and \n and \n\r and \r

so I went to dot net fiddle and tried this code to check where does the Environment.NewLine fail.

var myString = "this  \r\n is  \r some  \n text \n\r hurray";
var list = myString.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();

        foreach (var item in list){
            Console.WriteLine(item);    
}

the results were impressive but not so impressive, here are the results:

this is some text hurray

notice that Environment.NewLine missed \r escape character which is used by some popular OS including some old MAC OS.

so a better solution should be to use the following code with all the possible escape character in different operating systems:

somestring.Split(new[] { "\r\n", "\r", "\n", "\n\r"}, StringSplitOptions.None);

This will save you from weird scenarios.

Hope this helps!

to read more about what characters are used as escape characters in different operating systems visit: New Line Characters

5 Comments

This might not always work, there's no guarantee that a browser running on Windows, MacOS, Android, iOS, Linux, or any other OS would use the same convention for line endings as the server. Instead, enumerate the common possible ways that could be used.
@phuzi Thanks for sharing that point. can you please share where is the \r used as an escape character
Traditionally Apple Macs used \r as a line ending although Mac OS X uses \n asmost (all?) Unix/Linux based systems. Windows has always used \r\n.
@phuzi Thanks for sharing that I looked that up and also saw that some operating systems also use \n\r I need to update my answer to discuss that in detail. btw Yours is the most correct one here so +1 for that.
Wasn't aware of that combination, I'll it that to my answer ;)

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.