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

Time:12-06

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 >
        <label >Answer</label>
        <textarea asp-for="AnswerList" ></textarea>
    </div>

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

Can you guide me if you have a good solution?

CodePudding user response:

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 >
            <label >Answer</label>
            <textarea name="AnswerList"  style="text-align:right">@string.Join("\n ", Model.AnswerList)</textarea>
        </div>

result: enter image description here

  • Related