简体   繁体   中英

ASP.NET Core : Tag helper “select” is empty

I need to have a view with select element. I have the following VM class:

public class EditTicketVM
{
    public EditTicketVM()
    {
        Statuses = new SelectList(new List<SelectListItem>{
            new SelectListItem("Open", "open"),
            new SelectListItem("Pending", "pending"),
            new SelectListItem("Hold", "hold"),
            new SelectListItem("Solved", "solved"),
            new SelectListItem("Closed", "closed"),
        });

    }

    public string Description { get; set; }
    public string Subject { get; set; }
    public string Status { get; set; }

    public SelectList Statuses { get; set; }
}

and select element on page:

<select asp-for="Status" class="form-control" asp-items="Model.Statuses" />

But on result page this element is empty:

在此处输入图片说明

what is incorrect in my code?

I have tested your code on my side. Problem is in your select tag. You are using self closing select tag. This will not work. Write your select tag as follows:

<select asp-for="Status" class="form-control" asp-items="Model.Statuses">
   <option value="">Select Status</option>
</select>

You have also problem in your SelectList initialization. The way you have initialized SelectList would not work. Initialize your SelectList as follows:

public EditTicketVM()
{
   Statuses = new SelectList(new List<SelectListItem> {
       new SelectListItem(){Text = "Open", Value = "open"},
       new SelectListItem(){Text = "Pending", Value = "pending"},
       new SelectListItem(){Text = "Hold", Value = "hold"},
       new SelectListItem(){Text = "Solved", Value = "solved"},
       new SelectListItem(){Text = "Closed", Value = "closed"},
    },"Value","Text");
}

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