我正在從 MVC 視圖中發布值。下面是 .cshtml 代碼
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ContractStartDate" class="control-label"></label>
<input asp-for="ContractStartDate" class="form-control" />
<span asp-validation-for="ContractStartDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ContractEndDate" class="control-label"></label>
<input asp-for="ContractEndDate" class="form-control" />
<span asp-validation-for="ContractEndDate" class="text-danger"></span>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" asp-for="IsActive" /> @Html.DisplayNameFor(model => model.IsActive)
</label>
</div>
<div class="form-group">
<label asp-for="Website" class="control-label"></label>
<input asp-for="Website" class="form-control" />
<span asp-validation-for="Website" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="LogoUrl" class="control-label"></label>
<input asp-for="LogoUrl" class="form-control" />
<span asp-validation-for="LogoUrl" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
這是我的模型代碼
public class Client : BaseEntity
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage ="Client Name is Required")]
[Display(Name ="Client Name")]
public string Name { get; set; }
[Required(ErrorMessage = "Contract StartDate is Required")]
[DataType(DataType.Date, ErrorMessage = "Invalid Date Format")]
[Display(Name = "Contract StartDate")]
public DateTime ContractStartDate { get; set; }
[Required(ErrorMessage = "Contract EndDate is Required")]
[DataType(DataType.Date, ErrorMessage = "Invalid Date Format")]
[Display(Name = "Contract End Date")]
public DateTime ContractEndDate { get; set; }
[Required]
[Display(Name = "Is Active")]
public bool IsActive { get; set; }
[Required]
public string Website { get; set; }
public string LogoUrl { get; set; }
}
BaseEntity.cs 代碼
public abstract class BaseEntity
{
public string CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
public string CreatedIPAddress { get; set; }
public string ModifiedBy { get; set; }
public DateTime ModifiedDate { get; set; }
public string ModifiedIPAddress { get; set; }
}
這是 MVC 控制器中的 Post 函式。
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Name,ContractStartDate,ContractEndDate,IsActive,Website,LogoUrl")] Client client)
{
if (ModelState.IsValid)
{
_context.Add(client);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(client);
}
當我在提供所需值后單擊提交按鈕時......即使對于 BaseEntity.cs 類的非必需欄位,它也會顯示驗證錯誤。

當我填寫所有這些非必填欄位并從控制器的 post Bind[] 方法中洗掉時...顯示模型無效。

控制器模型驗證

請參閱詳細錯誤...

請幫助....如何繞過此錯誤。
uj5u.com熱心網友回復:
由于您使用的是 net 6,因此您有 2 個選擇
- 在 EF 核心專案中洗掉或注釋此行
<!--<Nullable>enable</Nullable>-->
- 或者手動使每個類的每個屬性都可以為空
public string? CreatedBy { get; set; }
public DateTime? CreatedDate { get; set; }
public string? CreatedIPAddress { get; set; }
// ---and so on for all classes
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/430886.html
標籤:C# asp.net-mvc 实体框架核心 asp.net-core-6.0
