Reputation: 511
I have a checkbox field in my form. when the form is edited and re-submitted, the value for the checkbox always passed to the controller is false. I am using 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);
}
I have tried other solutions/ways listed in stackoverflow. Nothing worked out. I am not sure what I was doing wrong?
Upvotes: 0
Views: 1149
Reputation: 20116
The reason is that your [Bind]
attribute does not include correct properties' names.
[Bind]
attribute specifies which properties of a model should be included in model binding.
Change to use IsDefault, IsStandard, IsEmailed
instead of 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)
Upvotes: 1
Reputation: 1339
Currently you are using below code to bind the data:
[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);
}
While your model ReprintEditViewModel
has properties IsDefault , IsStandard and IsEmailed
which are not included in Bind attribute. So MVC model binder will ignore those properties and bind only the properties passed in attribute. If you remove the Bind attribute then all the properties having same name as in Model will bind because of MVC default model binder and you will get the values.
You can learn more about model binding using this link
Upvotes: 1