sensei
sensei

Reputation: 7562

ASP.NET MVC show collection of data in View inside of TextArea

I access data with:

        public ActionResult Index()
    {

        //IEnumerable<ChatLogs> c = from p in db.ChatLogs select p;
        //return View(c);
        using (var db = new ChatLogContext())
        {
            var list = db.ChatLogs.ToList();

            return View(list);

        }

    }

I would like to know how to save this collection of data inside of TextArea in View? When we used webforms we could just textBox.Text = textBox.Text + "some data from database";

View:

@model IEnumerable<Chat.Models.ChatLogs>
@Html.TextArea("chatScreen", new { @Class = "chatScreen" })

Thank you.

Upvotes: 0

Views: 1109

Answers (2)

Theodoros Chatzigiannakis
Theodoros Chatzigiannakis

Reputation: 29213

I'd suggest that you create a view model. For example:

class ChatLogsViewModel
{
    public string LogListString { get; set; }
}

Pass that to the view, instead of passing the raw list:

var list = db.ChatLogs.ToList();
var vm = new ChatLogsViewModel { LogListString = /* convert list to single string here */ };
return View(vm);

And in the view, just do something like this:

@model Your.Namespace.ChatLogsViewModel
@Html.TextAreaFor(model => model.LogListString)

Using view models will make your life easier as soon as you decide that you want to pass more information to the view than what a single domain model can carry.

Upvotes: 2

U.P
U.P

Reputation: 7442

In you .cshtml view, you can access data using @Model

Now, since you have a list, I'd recommend you join it and then assign it to TextArea like

@{var strList = string.Join(" ", Model)}
@Html.TextArea("myTextArea",strList)

Upvotes: 1

Related Questions