Search code examples
asp.net-corerazor-pages

Asp.Net Core - Display string list values in textarea?


I have a list whose values ​​are taken from the database,I want each of these values ​​to be displayed in a line in textarea...

Controller :

 public async Task<IActionResult> AddOrEditPoll(Guid Id)
    {            
        var polloptionList = await _admin.GetQuestionsListByPollId(Id);
        PollViewModel model = new PollViewModel();    
        model.AnswerList = new List<string>();
        foreach (var item in polloptionList)
        {
            model.AnswerList.Add(item.Answer);
        };
        return View(model);
    }

View :

<div class="form-group">
        <label class="control-label">Answer</label>
        <textarea asp-for="AnswerList" class="form-control"></textarea>
    </div>

ّI want it to be displayed as follows: enter image description here

Can you guide me if you have a good solution?


Solution

  • You can try to replace asp-for with id and name,asp-for will set the value of textarea with AnswerList,and then convert AnswerList to string.Here is a demo:

    Action:

    public IActionResult AddOrEditPoll() {
                PollViewModel model = new PollViewModel();
                model.AnswerList = new List<string> { "answer1", "answer2" , "answer3" };
                return View(model);
            }
    

    View:

     <div class="form-group">
                <label class="control-label">Answer</label>
                <textarea name="AnswerList" class="form-control" style="text-align:right">@string.Join("\n ", Model.AnswerList)</textarea>
            </div>
    

    result: enter image description here