选择标签ASP.NET Core没有项目

时间:2019-08-20 10:13:04

标签: c# asp.net-core

我有验证码

public class DeliveryViewModel
{
    public string City { get; set; }

    public IEnumerable<City> Cities { get; } = new[]
    {
        new City { Id = 0, Name = "город доставки" },
        new City { Id = 0, Name = "Moscow" },
        new City { Id = 0, Name = "Yaric" }
    };
}

public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
}

<div>
        @Html.DropDownListFor(o => o.City, new SelectList(Model.Cities, "Id", "Name"))
        <select id="store-theme" name="store-theme" asp-for="City" asp-items="@(new SelectList(Model.Cities,"Id","Name"))"></select>
</div>

DropDownList对于炒锅没问题,但选择标记可将其设为空列表。

只有我做这个才行

    <select>
        @foreach (City item in Model.Cities)
        {
            <option value=@item.Id>@item.Name</option>
        }
    </select>

如何解决?

1 个答案:

答案 0 :(得分:0)

尝试:

<select asp-for="City" asp-items="@Model.Cities.Select(c => new SelectListItem() { Value = c.Name, Text = c.Id.ToString() }))"></select>