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

时间:2019-01-21 15:42:09

标签: c# asp.net-mvc html.dropdownlistfor enumdropdownlistfor

我想做的事-

我有两个不同的数据库表(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; }

}

这是我的观点-

@model 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();
    }

2 个答案:

答案 0 :(得分:0)

MultipleViews类添加一个构造函数,并设置诸如此类的变量

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

在设置变量值之前尚未声明变量。

答案 1 :(得分:0)

SelectList没有符合您意图的overload方法。在HTML区域中,select元素具有值和描述,类似于KeyValuePair。在您的情况下,键和值都相同。为此,请尝试:

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