ViewModel 将 null 返回到表单中,我该如何解决这个问题

ViewModel returning null into form, how do I fix this

我似乎无法将我从表单输入的值输入到控制器中的操作 createEmployee 方法中我认为问题出在 ViewModel 上,我可能错了,但我该如何解决这个问题。当我 运行 项目时,我唯一能收到的是我生成的随机数和空变量,还有我如何将输入的员工添加回列表 附注我的员工列表很难编码

View.cs


 @using (Html.BeginForm("CreateEmployee", "/Employee", FormMethod.Post))
{
<div class="row">
 <div class="col-lg-2">First Name</div>
<div class="col-lg-10">
@Html.TextBoxFor(m => m.Employee.Name, new { @class = "form-control" })
</div>
</div>
<br />
<div class="row">
<div class="col-lg-2">Last Name</div>
<div class="col-lg-10">
@Html.TextBoxFor(m => m.Employee.Surname, new { @class = "form-control" })
 </div>
</div>
   <br />
<div class="row">
<div class="col-lg-2">Date of Birth</div>
<div class="col-lg-10">
@Html.TextBoxFor(m => m.Employee.DateOfBirth, new { @class = "form-control" })
</div>
</div>
<br />
<div class="row">
<div class="col-lg-2">Department</div>
<div class="col-lg-10">
@Html.TextBoxFor(m => m.Employee.Department, new { @class = "form-control" })
</div>
</div>
<br />
<div class="modal-footer">
<button type="submit" class="btn btn-primary center-block">Complete</button>
</div>

}

EmployeeViewModel

 public List<Fundi.HR.Web.Models.Employee> Employees { get; set; }
        public Employee Employee { get; set; }

        public EmployeeViewModel(List<Employee> found)
        {
            this.Employees = found;
        }

操作员工控制器

public ActionResult CreateEmployee(Employee emp)
        {
            if (ModelState.IsValid)
            {
                Random random = new Random();
                int num = random.Next();

                Employee empObj = new Employee();
                List<Employee> employees = new List<Employee>();

                empObj.EmployeeId = num;
                empObj.Name = emp.Name;
                empObj.Surname = emp.Surname;
                empObj.DateOfBirth = emp.DateOfBirth;
                empObj.Department = emp.Department;


                employees.Add(empObj);
                EmployeeViewModel em = new EmployeeViewModel(employees);
                return View("Index", em);
            }
            else
            {
                return RedirectToAction("Index");
            }

为了简单的测试,我使用了你的部分代码,让值可以被接受,你可以参考以下内容: 查看:

@model WebApplication38.Models.Employee
@{
    ViewBag.Title = "Index";
}

@using (Html.BeginForm("CreateEmployee", "Test", FormMethod.Post))
{
    <div class="row">
        <div class="col-lg-2">First Name</div>
        <div class="col-lg-10">
            @Html.TextBoxFor(m=>m.name, new { @class = "form-control" })
        </div>
    </div>
    <br />
    <div class="row">
        <div class="col-lg-2">Last Name</div>
        <div class="col-lg-10">
            @Html.TextBoxFor(m => m.Surname, new { @class = "form-control" })
        </div>
    </div>
    <br />
    <div class="row">
        <div class="col-lg-2">Date of Birth</div>
        <div class="col-lg-10">
            @Html.TextBoxFor(m => m.DateOfBirth, new { @class = "form-control" })
        </div>
    </div>
    <br />
    <div class="row">
        <div class="col-lg-2">Department</div>
        <div class="col-lg-10">
            @Html.TextBoxFor(m => m.Department, new { @class = "form-control" })
        </div>
    </div>
    <br />
    <div class="modal-footer">
        <button type="submit" class="btn btn-primary center-block">Complete</button>
    </div>

}

控制器和结果: