[英]ASP.NET Core MVC model argument values becoming null in HttpPost method
我已经搜索了几个小时来寻找我面临的问题,但找不到任何可以帮助我解决问题的东西。 我正在练习 ASP.NET 核心 MVC 并尝试发布 model,但它的值在 Z293CZ246FF9985DC6 上不断变为 null 这是我的模型:
public class Employee
{
[Key]
public int Id { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
[Range(16,82)]
public int Age { get; set; }
[Required]
[Range(0,350000)]
public decimal Salary { get; set; }
public int DepartmentId { get; set; }
public Department Department { get; set; }
public string GetFullName => $"{FirstName} {LastName}";
}
public class Department
{
[Key]
[Required]
public int Id { get; set; }
[Required]
public string DepartmentName { get; set; }
public string Description { get; set; } = "N/A";
public List<Employee> Employees { get; set; } = new List<Employee>();
}
这是有问题的表格:
@model Employee
@{
}
<form method="post">
<div>
<label asp-for="FirstName" class="form-label"></label>
<input asp-for="FirstName" class="form-control"/>
</div>
<div>
<label asp-for="LastName" class="form-label"></label>
<input asp-for="LastName" class="form-control" />
</div>
<div>
<label asp-for="Age" class="form-label"></label>
<input asp-for="Age" class="form-control" />
</div>
<div>
<label asp-for="Department.DepartmentName" class="form-label">Department</label>
<input asp-for="Department.DepartmentName" disabled class="form-control" />
</div>
<div>
<label asp-for="Salary" class="form-label"></label>
<input asp-for="Salary" class="form-control" />
</div>
<br />
<button type="submit" class="btn btn-primary">Add employee</button>
<a asp-action="ListDepartments">Cancel</a>
</form>
这是有问题的controller:
public class DepartmentController : Controller
{
private DepartmentEmployeeDatabaseContext dbContext { get; set; }
public DepartmentController(DepartmentEmployeeDatabaseContext ctx)
{
dbContext = ctx;
}
public IActionResult Index()
{
return View();
}
public IActionResult ListDepartments()
{
IEnumerable<Department> departments = dbContext.Departments;
return View(departments);
}
public IActionResult AddEmployee(int? id)
{
Department department = new Department();
Employee employee = new Employee();
try
{
department = dbContext.Departments.Find(id);
employee.Department = department;
employee.DepartmentId = department.Id;
}
catch (Exception e)
{
return View();
}
return View(employee);
}
[HttpPost]
[AutoValidateAntiforgeryToken]
public IActionResult AddEmployee(Employee emp)
{
if (ModelState.IsValid)
{
dbContext.Add(emp);
dbContext.SaveChanges();
return RedirectToAction("ListDepartments");
}
return View(emp);
}
}
IActionResult AddEmployee(int? id) 方法工作正常(将部门和部门 ID 分配给新员工对象),但值在公共 IActionResult AddEmployee(Employee emp) 方法中变为 null。 我到底做错了什么? 提前致谢!
如果您将其更改为,您可以获得结果
@model Employee
<form method="post">
<div>
<label asp-for="@Model.FirstName" class="form-label"></label>
<input asp-for="@Model.FirstName" class="form-control"/>
</div>
<div>
<label asp-for="@Model.LastName" class="form-label"></label>
<input asp-for="@Model.LastName" class="form-control" />
</div>
<div>
<label asp-for="@Model.Age" class="form-label"></label>
<input asp-for="@Model.Age" class="form-control" />
</div>
<div>
<label asp-for="@Model.Department.DepartmentName" class="form-label">Department</label>
<input asp-for="@Model.Department.DepartmentName" disabled class="form-control" />
</div>
<div>
<label asp-for="@Model.Salary" class="form-label"></label>
<input asp-for="@Model.Salary" class="form-control" />
</div>
<button type="submit" asp-controller="Department" asp-action="AddEmployee" class="btn btn-primary">Add employee</button>
<form>
首先disabled
属性不会提交这个<input>
标签,所以你可以尝试使用readonly
属性。 将帮助asp-for
需要属性的名称标记为键,您可以像这样更改代码:
<div>
<label asp-for="Department.DepartmentName" class="form-label">Department</label>
<input asp-for="Department.DepartmentName" readonly class="form-control" />
<input asp-for="Department.Id" type="hidden" />
<input asp-for="Department.Description" type="hidden" />
<input asp-for=DepartmentId type="hidden" />
</div>
然后你可以得到Department
和DepartmentId
的值:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.