在 ASP.net 核心中填充下拉列表

Populating Dropdown in ASP.net Core

有谁知道如何处理 Asp.net 核心中的下拉菜单。我想我让自己很难理解新的 Asp.net 核心概念。 (我是 Asp.net Core 的新手)。

我有模型叫做 DriverVehicle。基本上可以在 master 中创建一堆车辆,然后将其附加到 Driver。这样 driver 将与车辆相关联。

我的问题是我还在某些区域使用 viewmodel 来组合两个不同的模型。(从默认模板中了解很少)

我的问题是我不知道下一步是什么,因为 ASP.net Core 是最新的,没有很多教程和 Q/As 可用。

Driver 型号

public class Driver
{
    [Required]
    public int Id { get; set; }

    [Required]
    public string ApplicationUserId { get; set; }

    [Required]
    public int VehicleId { get; set; }

    [Required]
    public string Status { get; set; }


    public virtual ApplicationUser ApplicationUser { get; set; }
    public virtual Vehicle Vehicle { get; set; }
}

车辆型号

public class Vehicle
{

    [Required]
    public int Id { get; set; }

    [Required]
    public string Make { get; set; }
    public string Model { get; set; }

    [Required]
    public string PlateNo { get; set; }
    public string InsuranceNo { get; set; }

}

Driver

的视图模型
    public class DriverViewModel
{
    [Required]
    [Display(Name = "ID")]
    public int ID { get; set; }

    [Required]
    [Display(Name = "User ID")]
    public string ApplicationUserId { get; set; }

    [Required]
    [Display(Name = "Vehicle ID")]
    public IEnumerable<Vehicle> VehicleId { get; set; }
    //public string VehicleId { get; set; }

    [Required]
    [Display(Name = "Status")]
    public string Status { get; set; }


}

这是我的观点

<div class="col-md-10">
   @*<input asp-for="VehicleId" class="form-control" />*@
   @Html.DropDownList("VehicleId", null, htmlAttributes: new { @class = "form-control"})
   <span asp-validation-for="VehicleId" class="text-danger" />
</div>

查看文档,似乎 asp.net 核心可能正在从 HTML 助手转向使用标签助手。以下 link 应该会有所帮助

https://docs.asp.net/en/latest/mvc/views/working-with-forms.html#the-select-tag-helper

特别是

@model CountryViewModel

<form asp-controller="Home" asp-action="Index" method="post">
<select asp-for="Country" asp-items="Model.Countries"></select> 
<br /><button type="submit">Register</button>
</form>

请注意 "asp-for" 的使用,它引用要绑定的模型属性,以及 "asp-items" 的使用,它引用 Select 列表项的模型属性源以及它是如何绑定的应用于 select 标签

为了完整性,下面引用了文档中使用的示例模型

namespace FormsTagHelper.ViewModels
{
public class CountryViewModel
{
    public string Country { get; set; }

    public List<SelectListItem> Countries { get; } = new List<SelectListItem>
    {
        new SelectListItem { Value = "MX", Text = "Mexico" },
        new SelectListItem { Value = "CA", Text = "Canada" },
        new SelectListItem { Value = "US", Text = "USA"  },
    };
}
}

@model EmployeeViewModel

<form asp-controller="Home" asp-action="SaveEmployee">
  <label asp-for="DesirablePosition">Desirable Position</label>
  <select asp-for="DesirablePosition" asp-items="Model.DesirablePositionItems"></select>
</form>

public class EmployeeViewModel {

public string DesirablePosition { get; set; }

public List<SelectListItem> DesirablePositionItems { get; } = new List<SelectListItem> {

        new SelectListItem { Value = "j", Text = "Junior" },

        new SelectListItem { Value = "m", Text = "Middle" },

        new SelectListItem { Value = "s", Text = "Senior" },
    };

}

public class 家庭控制器:控制器 {

public IActionResult SaveEmployee(){

    return View(new EmployeeViewModel());
}

}

Driver 控制器

public class DriverController : Controller
{
    private readonly ApplicationDBContext _context;

    public DriverController(ApplicationDBContext context)
    {
        _context = context;
    }

    // GET: Driver/Create
    public IActionResult Create()
    {
        ViewData["VehicleId"] = new SelectList(_context.Vehicle, "Id", "PlateNo");
        return View();
    }
}

Driver 查看

@model DriverViewModel
<form asp-action="Create">
    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    <div class="form-group">
        <label asp-for="VehicleId" class="control-label"></label>
        <select asp-for="VehicleId" class ="form-control" asp-items="ViewBag.VehicleId"></select>
    </div>
</form>