【问题标题】:Razor Checkboxes always return false in ASP.Net Core 2.2 MVCRazor Checkboxes 在 ASP.Net Core 2.2 MVC 中总是返回 false
【发布时间】:2019-07-10 08:59:14
【问题描述】:

我的表单中有一个复选框字段。当表单被编辑并重新提交时,传递给控制器​​的复选框的值总是为 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 中列出的其他解决方案/方法。什么都没解决。我不确定我做错了什么?

【问题讨论】:

  • 从您的操作方法中删除 [Bind] 属性并尝试。它应该工作:)
  • 非常感谢@Manprit。有效。你介意吗,你能解释一下删除绑定属性是如何工作的吗?

标签: c# asp.net-mvc asp.net-core razor asp.net-core-mvc


【解决方案1】:

目前您正在使用以下代码绑定数据:

[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 属性,那么所有与 Model 中同名的属性都将绑定,因为 MVC 默认模型绑定器,您将获得值。

您可以使用this link了解有关模型绑定的更多信息

【讨论】:

    【解决方案2】:

    原因是您的[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)
    

    参考https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.2#bind-attribute

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 2016-05-12
      • 2020-10-04
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多