显示来自数据库的下拉列表

Displaying dropdownlistfor from database

我正在尝试做的事情--

我有两个不同的数据库表(CabinetI、AdminCabinetI)。 AdminCabinetI 填充了必须作为下拉列表显示给用户的数据(列名称 ItemID)。一旦用户填写了其他信息,从下拉列表中进行选择并点击提交按钮,该数据就会进入 CabinetI。

当我添加 Dropdownlistfor 时,它开始抛出错误。我尝试了很多不同的方法,但没有任何效果。所以在这一点上,我想展示我的代码,看看我做错了什么。

这是我的 ViewModel --

public class MultipleViews
{
    public Note note { get; set; }

    public AdminCabinetI admincabinetI { get; set; }

    public CabinetI cabineti { get; set; }

    public IEnumerable<AdminCabinetI> SelectSerialsI { get; set; }
}

这是我的模型 (AdminCabinetI) 和 (CabinetI) --

public class AdminCabinetI
{
    [Key]
    public int ItemNo { get; set; }

    [Required(ErrorMessage = "Please enter item title")]
    public string ItemName { get; set; }

    [Required(ErrorMessage = "Please enter Item Serial number/ID")]
    public string ItemID { get; set; }

    [Required(ErrorMessage = "Please select cabinet status")]
    public string ItemStatus { get; set; }

    public string BA { get; set; }

    public string Printer { get; set; }
}

public class CabinetI
{
    [Key]
    public int CabinetNo { get; set; }

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

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

    [Required(ErrorMessage = "Please enter your name")]
    public string UserName { get; set; }

    [Required(ErrorMessage = "Please select one of cabinet serial numbers")]
    public string CabinetSerial { get; set; }

    [Required(ErrorMessage = "Please select cabinet status")]
    public string CabinetStatus { get; set; }

    [Required(ErrorMessage = "Please type specify cabinet location")]
    public string CabinetLocation { get; set; }

}

这是我的观点 --

@模特PreMode.ViewModels.MultipleViews

    <div class="form-group">
        <label>Category</label>
        <input type="text" readonly="readonly" class="form-control" style="opacity: 0.6" value="I2" asp-for="cabineti.CabinetName">
    </div>

    <div class="form-group">
        <label>Department</label>
        <select class="form-control" asp-for="cabineti.Department">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select>
    </div>

    <div class="form-group">
        <label>Name</label>
        <input type="text" class="form-control" asp-for="cabineti.UserName" placeholder="Please enter your name" />
        <span class="text-danger" asp-validation-for="cabineti.UserName"></span>
    </div>

    <div class="form-group">
        <label>Serial Number</label>
        @Html.DropDownListFor(model => model.admincabinetI, new SelectList(Model.admincabinetI.ItemID, "ItemID"), "Select Cabinet Serial #", new { @class = "form-control" })

    </div>

    <div class="form-group">
        <label>Status</label>
        <select class="form-control" asp-for="cabineti.CabinetStatus">
            <option value="In Use">In Use</option>
            <option value="Not In Use">Not In Use</option>
            <option value="Testing">Testing</option>
        </select>
        <span class="text-danger" asp-validation-for="cabineti.CabinetStatus"></span>
    </div>

    <div class="form-group">
        <label>Location</label>
        <input type="text" class="form-control" asp-for="cabineti.CabinetLocation" placeholder="Please type current location of the cabinet" />
        <span class="text-danger" asp-validation-for="cabineti.CabinetLocation"></span>
    </div>


    <div class="form-group">
        <button type="submit" class="btn btn-primary">Checkout</button>
        <a class="btn btn-warning" href="/Cabinet/MainCabinetI">Cancel</a>
    </div>
</form>

这是我的控制器--

    public IActionResult GetDropDown()
    {
        if (ModelState.IsValid)
        {

            using (var db = new DataMigration())
            {
                var CabinetSerialsI = db.AdminCabinetI.ToList();

                var viewModel = new MultipleViews
                {
                    SelectSerialsI = CabinetSerialsI
                };
                return View(viewModel);
            }
        }
        return View();
    }

MultipleViewsclass中添加一个构造函数并设置变量,例如

public MultipleViews()
{
    this.Note  = new Note();
    this.AdminCabinetI  = new AdminCabinetI ();
    this.CabinetI  = new CabinetI ();
    this.SelectSerialsI  = new List<AdminCabinetI>();
}

您在设置变量值之前没有声明变量。

SelectList 没有符合您意图的 overload 方法。在 HTML 中,一个 select 元素同时具有值​​和描述,类似于 KeyValuePair。在您的情况下,键和值都是相同的。为了解决这个问题,请尝试:

SelectList(Model.admincabinetI.ItemID, "ItemID", "ItemID")