简体   繁体   English

Razor复选框在ASP.Net Core 2.2 MVC中始终返回false

[英]Razor Checkboxes always return false in ASP.Net Core 2.2 MVC

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. 当编辑并重新提交表单时,始终传递给控制器​​的复选框的值为false。 I am using ASP.Net MVC Core 2.2. 我正在使用ASP.Net MVC Core 2.2。

Edit.cshtml 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 ViewModel.cs

public class ReprintEditViewModel
{
    public bool IsDefault { get; set; }
    public bool IsStandard { get; set; }
    public bool IsEmailed { get; set; }
}

Controller.cs 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. 我尝试了stackoverflow中列出的其他解决方案/方式。 Nothing worked out. 什么都没解决。 I am not sure what I was doing wrong? 我不确定自己做错了什么?

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. 当您的模型ReprintEditViewModel具有属性IsDefault , IsStandard and IsEmailed ,它们不包含在Bind属性中。 So MVC model binder will ignore those properties and bind only the properties passed in attribute. 因此,MVC模型绑定程序将忽略这些属性,而仅绑定传入属性的属性。 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. 如果删除Bind属性,则由于MVC默认模型绑定程序,将绑定所有具有与Model中相同名称的属性,并且您将获得这些值。

You can learn more about model binding using this link 您可以使用此链接了解有关模型绑定的更多信息

The reason is that your [Bind] attribute does not include correct properties' names. 原因是您的[Bind]属性未包含正确的属性名称。

[Bind] attribute specifies which properties of a model should be included in model binding. [Bind]属性指定模型绑定中应包含模型的哪些属性。

Change to use IsDefault, IsStandard, IsEmailed instead of Default, Standard, Emailed 更改为使用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)

Refer to https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.2#bind-attribute 请参阅https://docs.microsoft.com/zh-cn/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.2#bind-attribute

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net 核心 razor 页面复选框 model 绑定(始终为假) - asp.net core razor page checkboxes model binding (always false) IFormFile 始终为空(带有 MVC/Razor 的 ASP.NET Core) - IFormFile always null (ASP.NET Core with MVC/Razor) 本地化文件无法在MVC ASP.NET Core 2.2中呈现Razor页面 - Localization file not effective rendering a Razor page in MVC ASP.NET Core 2.2 .net core 2.2 MVC:Razor模型返回空引用异常 - .net core 2.2 MVC: Razor Model return null reference exception 使用 Razor Pages 在 Asp.NET Core 2.2 中找不到 ViewComponent - ViewComponent not found in Asp.NET Core 2.2 using Razor Pages 在razor,asp.net Core 2.2页面之间传递值 - Passage of values ​between pages razor, asp.net Core 2.2 Razor 对 asp.net 核心 2.2 中的 Controller 的页面表单操作 - Razor Page form action to Controller in asp.net core 2.2 ASP.NET MVC 复选框始终为 false - ASP.NET MVC checkbox always false 即使在使用asp.net core 2.2中的PasswordSignInAsync成功登录后,User.Identity.IsAuthenticated也始终返回false - User.Identity.IsAuthenticated always returns false even after successfully logged in using PasswordSignInAsync in asp.net core 2.2 ModelState.IsValid 在 ASP.NET Core 6.0 MVC 中总是返回 false - ModelState.IsValid always returns false in ASP.NET Core 6.0 MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM