在ASP.net Core中填充下拉列表 [英] Populating Dropdown in ASP.net Core

查看:1037
本文介绍了在ASP.net Core中填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何处理Asp.net核心中的Dropdowns.我想我很复杂,难以理解新的Asp.net核心概念. (我是Asp.net Core的新手.)

Does anyone know how to deal with Dropdowns in Asp.net core. I think I made myself very complicated to understand the new Asp.net core concept. (I am new to Asp.net Core).

我有一个名为DriverVehicle的模型.基本上,可以在母版中创建一堆车辆,然后将其附加到驱动程序.这样驾驶员才能与车辆关联.

I have models called Driver, Vehicle. Basically one can create bunch of vehicles in the master then attach it to the Driver. So that the driver will be associated with a vehicle.

我的问题是我还在某些地方使用viewmodel来组合两个不同的模型.(默认模板对此了解甚少)

My problem is that I am also using viewmodel in some area to combine two different models.(understood little from default template)

我的问题是,由于ASP.net Core是最新的,我不知道下一步是什么,没有太多的教程和Q/As.

My problem is I have no idea what is the next step since ASP.net Core is very latest, there are not a lot of tutorials and Q/As available.

驱动器型号

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

}

驱动程序的ViewModel

ViewModel of 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帮助程序转向使用标签帮助程序.以下链接应该有帮助

Looking at the documentation it appears that asp.net core may be moving away from HTML helpers and towards the use of tag helpers. The following link should help

>://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"引用选择列表项"列表的模型属性源,以及如何将其应用于选择标签

Note the use of "asp-for" which references the Model attribute to bind and the use of "asp-items" which references the model attribute source for the List of Select List items and how it is applied to the select tag

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

The sample model used in the documentation is referenced below for completeness

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"  },
    };
}
}

这篇关于在ASP.net Core中填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆