我是 asp.net 核心的初學者,剛剛開始了一個 EmployeeManagement 專案,其中 Employee 是我的基類:
public class Employee
{
public int Id { get; set; }
[Required]
[Display(Name="First Name")]
public string Name { get; set; }
[Required]
[Display(Name="Official Email ID")]
[RegularExpression(@"^[a-zA-Z0-9_. -] @[a-zA-Z0-9-] \.[a-zA-Z0-9-.] $",
ErrorMessage = "Invalid email format")]
public string Email { get; set; }
public string Department { get; set; }
}
DropdownListModel 是我的第二個類,它繼承自基類員工:
public class DropdownListModel:Employee
{
public SelectList Departmentcollection { get; set; }
}
Departmentcollection當我創建新員工時,用于將專案從資料庫中填充到下拉串列中。
<form asp-controller="home" asp-action="CreateEmp" method="post">
<div class="form-group">
<div>
<label asp-for="Name"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div>
<label asp-for="Email"></label>
<input asp-for="Email" class="form-control">
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div>
<label asp-for="Department"></label>
<select asp-for="Department"
asp-items=@Model.Departmentcollection class="form-control"></select>
</div>
<div>
<button type="submit" class="btn btn-dark" >Create</button>
</div>
[HttpPost]
public IActionResult CreateEmp(Employee emp)
{
if(!ModelState.IsValid)
{
return View();
}
Employee NewEmployee= repository.AddEmployee(emp);
return RedirectToAction("Details",new { Id = NewEmployee.Id });
}
[HttpGet]
public ViewResult CreateEmp()
{
return View (new DropdownListModel { Departmentcollection
= new SelectList(
repository
.employees.Select(d => d.Department).Distinct())
});
}
代碼作業正常,下拉串列正在填充并且記錄正在插入。
問題是驗證不起作用。
我得到的錯誤是:
An unhandled exception occurred while processing the request.
NullReferenceException: Object reference not set to an instance of an object.
AspNetCore.Views_Home_CreateEmp.<ExecuteAsync>b__19_0() in
CreateEmp.cshtml
[email protected] class="form-control"></select>
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext.GetChildContentAsync(bool useCachedResult, HtmlEncoder encoder)
我究竟做錯了什么?
我使用了虛擬和覆寫屬性仍然是同樣的錯誤。
services.AddControllersWithViews(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true);
uj5u.com熱心網友回復:
處理請求時發生未處理的例外。NullReferenceException:物件參考未設定為物件的實體。
NullReferenceException是由空物件值引起的。在您的場景中,它是由[email protected]您的詳細錯誤訊息引起的。
假設您的后端代碼如下:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> CreateEmp(DropdownListModel obj)
{
//do your stuff to add to database.....
return View(obj);
}
您可以看到您的錯誤訊息是CreateEmp.cshtml在第一次填充的,并且您說下拉串列正在填充。也就是說,您的代碼必須逐步執行代碼return View(obj),在這種情況下,它會回傳以CreateEmp.cshtml再次渲染,可能會導致一些問題。你可以除錯你的代碼來檢查你收到的模型,發現Departmentcollection. 因為表單提交時<select asp-for="Department">會將選定的值發送到屬性。Department
最重要的是,您需要在回傳視圖之前再次重置下拉串列值,如下所示:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> CreateEmp(DropdownListModel obj)
{
if(!ModelState.IsValid)
{
var data = new DropdownListModel()
{
Id = obj.Id,
Department = obj.Department,
Email = obj.Email,
Name = obj.Name,
Departmentcollection = new SelectList(repository.employees.Select(d => d.Department).Distinct())
};
return View(data);
}
Employee NewEmployee= repository.AddEmployee(emp);
return RedirectToAction("Details",new { Id = NewEmployee.Id });
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/484675.html
