簡體   English   中英

ASP.NET Core MVC model 參數值在 HttpPost 方法中變為 null

[英]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>

然后你可以得到DepartmentDepartmentId的值:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM