在 .NET Core MVC 6 中发布表单时,模型绑定强制在非必填字段中具有值
Model Binding is forcing to have value in non-required field while posting the form in .NET Core MVC 6
我正在 post 从 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 class.
的非必需字段,它也会显示验证错误
当我填写所有这些非必填字段并从控制器的 post Bind[] 方法中删除时...显示模型无效。
控制器模型验证
查看详细错误...
请帮忙....如何绕过这个错误。
由于您使用的是 net 6,因此您有 2 个选择
- 在 EF 核心项目中删除或注释这一行
<!--<Nullable>enable</Nullable>-->
- 或手动使每个 class 中的每个 属性 可为空
public string? CreatedBy { get; set; }
public DateTime? CreatedDate { get; set; }
public string? CreatedIPAddress { get; set; }
// ---and so on for all classes
我正在 post 从 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 class.
的非必需字段,它也会显示验证错误当我填写所有这些非必填字段并从控制器的 post Bind[] 方法中删除时...显示模型无效。
控制器模型验证
查看详细错误...
请帮忙....如何绕过这个错误。
由于您使用的是 net 6,因此您有 2 个选择
- 在 EF 核心项目中删除或注释这一行
<!--<Nullable>enable</Nullable>-->
- 或手动使每个 class 中的每个 属性 可为空
public string? CreatedBy { get; set; }
public DateTime? CreatedDate { get; set; }
public string? CreatedIPAddress { get; set; }
// ---and so on for all classes