![](/img/trans.png)
[英]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.