asp.net 如何使用实体框架进行选择(在html中使用select输入选择两个字符串中的一个)?

rqenqsqc  于 2022-12-15  发布在  .NET
关注(0)|答案(1)|浏览(91)

我想让用户只在两个字符串中选择一个,Internal或Consigned,插入到数据库的InternalConsigned列中。我该怎么做呢?这是我当前的代码:
Equipment.cs模型类:

public class Equipment
    {
        [Key]
        public int Id { get; set; }

        [Required]
        [DisplayName("Equipment Name")]
        public string Name { get; set; }

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

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

        [ForeignKey("DepartmentId")]
        public int DepartmentId { get; set; }
        public Department? Department { get; set; }

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

        public DateTime EOLDate { get; set; }

    }

创建操作方法:

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,Name,Amount,Status,DepartmentId,InternalConsigned,EOLDate")] Equipment equipment)
        {
            if (ModelState.IsValid)
            {
                _context.Add(equipment);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            ViewData["DepartmentId"] = new SelectList(_context.Departments, "Id", "Name", equipment.DepartmentId);
            return View(equipment);
        }

Create.cshtml:

@model Equipment

@{
    ViewData["Title"] = "Create";
}

<h4>Equipment</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Name" class="control-label"></label>
                <input asp-for="Name" class="form-control" />
                <span asp-validation-for="Name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Amount" class="control-label"></label>
                <input asp-for="Amount" class="form-control" />
                <span asp-validation-for="Amount" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Status" class="control-label"></label>
                <input asp-for="Status" class="form-control" />
                <span asp-validation-for="Status" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="DepartmentId" class="control-label"></label>
                <select asp-for="DepartmentId" class ="form-control" asp-items="ViewBag.DepartmentId"></select>
            </div>
            <div class="form-group">
                <label asp-for="InternalConsigned" class="control-label"></label>
                <input asp-for="InternalConsigned" class="form-control" />
                <span asp-validation-for="InternalConsigned" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="EOLDate" class="control-label"></label>
                <input asp-for="EOLDate" class="form-control" />
                <span asp-validation-for="EOLDate" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

我知道我必须在视图中使用select标记,但我不确定在控制器中写入什么。

qv7cva1a

qv7cva1a1#

如果我没有误解你的问题,你想写一个下拉列表两个选项InternalConsigned,所以你可以参考这个简单的演示,希望它能帮助你.

List<SelectListItem> test = new List<SelectListItem>();
            test.Add(new SelectListItem { Text = "Internal ", Value = "Internal " });
            test.Add(new SelectListItem { Text = "Consigned", Value = "Consigned" });
            ViewData["demo"] = test;

然后在视图中:

<select asp-for="InternalConsigned" asp-items="@ViewBag.demo"></select>

演示:

相关问题