【发布时间】:2021-04-10 11:23:30
【问题描述】:
如果我与员工一起工作,我正在处理应用程序。 每个员工都有自己的 ID,他们在创建时收到。
但我也在处理不同的角色,例如 Mananger Id。 我有一个表单,此时它只是一个普通的输入表单,用户可以在其中输入任何类型的数字,例如“12515215”。
我想改为在该表单中选择现有的员工 ID。因此,用户别无选择,只能选择数据库中已经存在的那些 Id 之一。 这是我的控制器操作:
region Create Employees
public IActionResult AddEmployee()
{
return View();
}
// Create employees
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddEmployee([Bind("Id,FirstName,LastName,Salary,IsCEO,IsManager,ManangerId")] Employee employee)
{
if (ModelState.IsValid)
{
_context.Add(employee);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(employee);
}
#endregion
这就是 Razorpage 的样子。
@model TheLibrary.Models.Employee
@{
ViewData["Title"] = "Add New Employee";
}
<h1>Add new Employee</h1>
<h4>Employee</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="AddEmployee">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="FirstName" class="control-label"></label>
<input asp-for="FirstName" class="form-control" />
<span asp-validation-for="FirstName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="LastName" class="control-label"></label>
<input asp-for="LastName" class="form-control" />
<span asp-validation-for="LastName" class="text-danger"></span>
</div>
<div class="form-group">
<p>Add a rank number between 1-10 to calculate salary</p>
<label asp-for="Salary" class="control-label"></label>
<input asp-for="Salary" class="form-control" />
<span asp-validation-for="Salary" class="text-danger"></span>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" asp-for="IsCEO" /> @Html.DisplayNameFor(model => model.IsCEO)
</label>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" asp-for="IsManager" /> @Html.DisplayNameFor(model => model.IsManager)
</label>
</div>
<div class="form-group">
<label asp-for="ManangerId" class="control-label"></label>
<input asp-for="ManangerId" class="form-control" />
<span asp-validation-for="ManangerId" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Add Employee" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
还有模特
public class Employee
{
public int Id { get; set; }
[DisplayName("First Name")]
[Required]
public string FirstName { get; set; }
[DisplayName("Last Name")]
[Required]
public string LastName { get; set; }
[Range(1, 10)]
[Required]
public decimal Salary { get; set; }
public bool IsCEO { get; set; }
public bool IsManager { get; set; }
public int ManangerId { get; set; }
}
主 ID 键和这个“ManangerId”是同一模型的一部分。 关于如何更改它的任何想法,以便我可以在我的 razorpage 中将现有的 ID 添加为选择而不是普通的输入表单?
【问题讨论】:
-
你必须展示你的控制器动作和一个完整的视图。还有 Manager 和 User 类。
-
您好,感谢您的评论!我使用所有相关代码、控制器操作和完整的剃须刀页面以及模型类编辑了我的问题。
标签: c# asp.net-core razor asp.net-core-mvc