简体   繁体   中英

How to return a list of model items to an ASP.NET Core SelectListItem?

I have a model defined as:

public class MessageModel
{
    private Dictionary<string, MessageAttributeValue> messageAttributes;

    /// <summary>
    /// This model class is used to store the contents of the SQS Message Responses for the ListAllTemplates command.
    /// It does not map into a physical database table, rather stored in memory for use by the DashboardController.
    /// </summary>
    public MessageModel() { }

    public MessageModel(string messageId, string body, Dictionary<string, MessageAttributeValue> messageAttributes, string receiptHandle)
    {
        MessageId = messageId;
        Body = body;
        this.messageAttributes = messageAttributes;
        ReceiptHandle = receiptHandle;
    }

    public string Message { get; set; }
    public string MessageId { get; set; }
    public string MessageHandler { get; set; }
    public string Body { get; set; }
    public string ReceiptHandle { get; set; }

    public List<SelectListItem> Messages { get; } = new List<SelectListItem>();
}

It is used to store SQS Message Responses and is referenced in my controller as follows:

 public async Task<IActionResult> Index()
    {
        var models = new List<MessageModel>();
        await ListTemplatesAsync();
        await StartEnvironmentAsync();
        return View(models);
    }
 private async Task ListTemplatesAsync()
    {
        var response = await GetEnvironments.OnMessageReceiptAsync();
        var models = new List<MessageModel>();
        foreach (var msg in response)
        {
            models.Add(new MessageModel(msg.MessageId, msg.Body, msg.MessageAttributes, msg.ReceiptHandle));
        }
    }

I want to return a list of MessageIds from MessageModel which is holding the SQS responses.

My view markup is as follows:

@model IEnumerable<MySite.Models.MessageModel>;
    }
    <form id="frmEnvironment" asp-controller="Dashboard" method="get">
          <select id="selTemplates" asp-items="Messages"></select>
          <button id="btnStart" role="button" class="btn btn-primary" type="button" title="Start"></button><br />
          <button id="btnStop" class="btn btn-primary" type="button" title="Stop"></button><br />
          <button id="btnTest" class="btn btn-primary" title="Test" type="button"></button><br />
   </form>

How do I ensure that a list of MessageModel items is returned in the select dropdown list?

you should fill your model , i think you return an empty model

public async Task<IActionResult> Index()
{
        List<MessageModel> models =await ListTemplatesAsync();
        await StartEnvironmentAsync();
        return View(models);
}
private async Task<List<MessageModel>> ListTemplatesAsync(List<MessageModel> models)
{
    var response = await GetEnvironments.OnMessageReceiptAsync();
    List<MessageModel> models = new List<MessageModel>();
    foreach (var msg in response)
    {
        models.Add(new MessageModel(msg.MessageId, msg.Body, msg.MessageAttributes, msg.ReceiptHandle));
    }
    return models ;
}

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