[英]asp.net core razor page checkboxes model binding (always false)
[英]Razor Checkboxes always return false in ASP.Net Core 2.2 MVC
我的表单中有一个复选框字段。 当编辑并重新提交表单时,始终传递给控制器的复选框的值为false。 我正在使用ASP.Net MVC Core 2.2。
Edit.cshtml
<form asp-action="Edit">
<table>
<tr>
<td><label for="default">Default</label></td>
<td>@Html.CheckBoxFor(m => m.IsDefault, new { @checked = "checked", @class = "form-input-styled" })</td>
</tr>
<tr>
<td><label for="standard">Standard</label></td>
<td>@Html.CheckBoxFor(m => m.IsStandard, new { @checked = "checked", @class = "form-input-styled" })</td>
</tr>
<tr>
<td><label for="emailed">Emailed</label></td>
<td>@Html.CheckBoxFor(m => m.IsEmailed, new { @checked = "checked", @class = "form-input-styled" })</td>
</tr>
</table>
</form>
ViewModel.cs
public class ReprintEditViewModel
{
public bool IsDefault { get; set; }
public bool IsStandard { get; set; }
public bool IsEmailed { get; set; }
}
Controller.cs
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Date,PolicyNumber,OwnerName,SendTo,EmailAddress,ModifiedDate,LastModifiedBy,DeliveryMethod, Default, Standard, Emailed")] ReprintEditViewModel xrCertReprint)
{
if (ModelState.IsValid)
{
//string dm = string.Join(", ", DeliveryMethod);
string dm = "";
if (xrCertReprint.IsDefault == true)
dm = "Default";
if (xrCertReprint.IsStandard == true)
if (dm.Length > 1)
dm = dm + ", " + "Standard";
else
dm = "Standard";
if (xrCertReprint.IsEmailed == true)
if (dm.Length > 1)
dm = dm + ", " + "Emailed";
else
dm = "Emailed";
return RedirectToAction(nameof(Index));
}
return View(xrCertReprint);
}
我尝试了stackoverflow中列出的其他解决方案/方式。 什么都没解决。 我不确定自己做错了什么?
当前,您正在使用以下代码绑定数据:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Date,PolicyNumber,OwnerName,SendTo,EmailAddress,ModifiedDate,LastModifiedBy,DeliveryMethod, Default, Standard, Emailed")] ReprintEditViewModel xrCertReprint)
{
if (ModelState.IsValid)
{
//string dm = string.Join(", ", DeliveryMethod);
string dm = "";
if (xrCertReprint.IsDefault == true)
dm = "Default";
if (xrCertReprint.IsStandard == true)
if (dm.Length > 1)
dm = dm + ", " + "Standard";
else
dm = "Standard";
if (xrCertReprint.IsEmailed == true)
if (dm.Length > 1)
dm = dm + ", " + "Emailed";
else
dm = "Emailed";
return RedirectToAction(nameof(Index));
}
return View(xrCertReprint);
}
当您的模型ReprintEditViewModel
具有属性IsDefault , IsStandard and IsEmailed
,它们不包含在Bind属性中。 因此,MVC模型绑定程序将忽略这些属性,而仅绑定传入属性的属性。 如果删除Bind属性,则由于MVC默认模型绑定程序,将绑定所有具有与Model中相同名称的属性,并且您将获得这些值。
您可以使用此链接了解有关模型绑定的更多信息
原因是您的[Bind]
属性未包含正确的属性名称。
[Bind]
属性指定模型绑定中应包含模型的哪些属性。
更改为使用IsDefault, IsStandard, IsEmailed
而不是Default, Standard, Emailed
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Date,PolicyNumber,OwnerName,SendTo,EmailAddress,ModifiedDate,LastModifiedBy,DeliveryMethod, IsDefault, IsStandard, IsEmailed")] ReprintEditViewModel xrCertReprint)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.