为什么我的复选框未将其值传递给ASP.NET Core中的控制器? [英] Why do my checkboxes not pass their value to my controller in ASP.NET Core?

查看:67
本文介绍了为什么我的复选框未将其值传递给ASP.NET Core中的控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这可能很容易解决,但我似乎无法解决.我有一个ASP.NET Core Web应用程序,正在使用ajax表单将数据提交到控制器进行处理.我的问题是,即使我的复选框可能被选中,也始终以"false"传递.

I feel this may be an easy fix but I cannot seem to get around it. I have an ASP.NET Core web application and I'm using an ajax form to submit data to my controller for processing. The problem I have is that my checkboxes always pass in as "false" even though they may be checked.

我搜索了一些答案,但没有尝试过.诸如使用 checked 属性,以确保名称正确之类的事情.

I searched for some answers but nothing I've tried has worked. Things like using the checked property, ensuring the name is correct.

有人可以阐明在将表单提交给控制器时为什么会忽略这些值以及我该如何解决吗?

Can anyone shed some light on why the values are ignored when the form is submitted to the controller and how I can fix it?

表格

<form asp-action="CreateCustomView" asp-controller="Data"
        data-ajax="true"
        data-ajax-method="POST">
        <div class="row">
            <div class="col">
                <div class="custom-control custom-checkbox">                            
                    <input type="checkbox" class="custom-control-input" id="ColName" name="ColName" checked>
                    <label class="custom-control-label" for="ColName">Name</label>
                </div>
            </div>

            <button type="reset">Reset</button>
            <button type="submit">Save changes</button>
</form>

模型

namespace MyProject.Data
{
    public class GridView : BaseEntity
    {
        public string Name { get; set; }               
        public bool ColName { get; set; }
    }
}

DataController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MyProject.Data;
using Microsoft.AspNetCore.Mvc;

namespace MyProject.UI.Controllers
{
    public class DataController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult CreateCustomView(GridView data)
        {
            //This method is incomplete, there is a break at the ActionResult to see what values are passed into `data`.  In production, the values would be handled and the changes would be saved.
            return View();
        }
    }
}

我在方法 CreateCustomView 上有一个断点,以查看传递了什么值,无论我选中了哪些复选框,它们总是以 false 的形式传递.

I have a breakpoint on the method CreateCustomView to see what values are passed, no matter what checkboxes I check, they are always being passed as false.

有人可以帮助解决这个相当奇怪的问题吗?

Can anyone help with this rather strange problem?

更新

根据@ Reyan-Chougle提供的答案,如果您使用的是CORE,则需要按以下方式提供输入:

As per the answer supplied by @Reyan-Chougle, if you're using CORE you need to supply the input as follows:

<input asp-for="@Model.ColType" type="checkbox" />

呈现页面时,将自动为控件提供一个隐藏字段.以下是上述控件的HTML呈现为:

When the page is rendered, the control is automatically given a hidden field. Here is what the HTML of the above control renders as:

<input type="checkbox" class="custom-control-input" id="ColType" value="true" data-val="true" data-val-required="The ColType field is required." name="ColType">
<input type="hidden" value="false" id="ColType" name="ColType">

如您所见,它创建了一个值为false的隐藏复选框.这意味着,作为布尔类型,它在提交时始终具有true或false的值.

As you can see it creates a hidden checkbox with a value of false. This means that, as a boolean type it always has a value of true or false on submission.

推荐答案

在使用ASP.NET Core时,建议使用标记助手:

As you are using ASP.NET Core, it is recommend to use the Tag Helpers:

<div class="form-group">
    <label asp-for="@Model.ColName"></label>
    <input asp-for="@Model.ColName" type="checkbox" />
</div>

这篇关于为什么我的复选框未将其值传递给ASP.NET Core中的控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆