Asp.Net Core - 在 textarea 中显示字符串列表值?

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

我有一个列表,其值是从数据库中获取的,我希望这些值中的每一个都在textarea中显示在一行中...

控制器:

 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);
    }

查看:

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

ّ我希望它显示如下:

有好的解决方法可以指导一下吗?

你可以试试把asp-for换成idnameasp-for会将textarea的值设置成AnswerList,然后转换AnswerListstring。这是一个演示:

操作:

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

查看:

 <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>

结果: