简体   繁体   中英

C# multiple strings with line breaks in between into one TextAreaFor field (MVC)

I have 3 strings that have the following format(each on its own line):

"String 1"

"String 2"

"String 3"

What's the best way to recreate/transfer this format into a textareafor in my view (while still binding to a viewmodel property)?

You could use a view model:

public class MyViewModel
{
    public string Text { get; set; }
}

that will be passed to the view:

public ActionResult Index()
{
    var strings = new[] { "String 1", "String 2", "String 3" };
    var model = new MyViewModel
    {
        Text = string.Join(Environment.NewLine, strings)
    };
    return View(model);
}

for rendering:

@model MyViewModel
...
@Html.TextAreaFor(x => x.Text)

将字符串保存在属于viewmodel的IList中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM