复选框布尔数组如果被用户选中则不会带来真实值

Checkboxes array of bools doesnt brings the true value if it's checked by user

我有一个 bool 数组中的复选框列表,我希望我可以在该特定复选框中将 Checked 值作为 "true" 提供,如果它被用户选中的话

这是我的模型

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace testnumbers.Models
{
    public class StringNumber
    {
        [Required]
        public bool[] array = new bool[27];
        public double Numerito { get; set; }
        public double numero(bool[] array){
            int i = 0;
            double result=0.0;
            double temp=0.0;
            foreach (bool str in array){
                if( str == true)
                    temp=1.0;
                else
                    temp=0.0;
                result+= temp* System.Math.Pow(2,(double)i);
                i++;
            }
        return result;
        }
    }
}

我的控制器 2 个动作: 使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Web; 使用 System.Web.Mvc; 使用 testnumbers.Models;

    namespace testnumbers.Controllers
    {
        public class StringNumberController : Controller
        {
            // GET: StringNumber
            public ActionResult Index()
            {
                //StringNumber number = new StringNumber();
                return View(new StringNumber());
            }
            [HttpPost]
            public ActionResult Result(StringNumber number)
            {
                 //model not valid, do not save, but return current umbraco page
                if (ModelState.IsValid == false)
                {
                    RedirectToAction("index");
                }

                number.Numerito = number.numero(number.array);
                return View(number);
            }
        }
    }

和视图: @model testnumbers.Models.StringNumber @{ ViewBag.Title = "Index"; } 灵感来自:This post for Customs GPOs

       @using (Html.BeginForm("Result", "StringNumber", FormMethod.Post )) {  //,new { StringNumber = Model }
           @Html.AntiForgeryToken()
           string[] str = { "Z", "Y", "X", "W", "V", "U", "T", "S", "R", "Q", "P", "O", "N", "M", "L", "K", "J", "I", "H", "G", "F", "E", "D", "C", "B", "A" }; int i = 0; <table>
            <tr>
                @for (i = 0; i < Model.array.Count() - 1; i++)
                {
                    @Html.HiddenFor(m => m.array[i])

                    <td>@str[i] : @Html.CheckBoxFor(m => m.array[i])</td>
                }
            </tr>
        </table>

        <input type="submit" value="Submit"  />
    }

视图总是将 bool 数组的 thr 值设为 false,我想更新为 "true" 那些选择给控制器并可以完成工作的复选框。

提前致谢

移除您为相同内容创建的隐藏输入 属性

@Html.HiddenFor(m => m.array[i])

DefaultModelBinder 读取第一个 name/value 对匹配您的 属性 名称并绑定它。任何后续的 name/value 对都将被忽略,因此

创建的输入值
@Html.CheckBoxFor(m => m.array[i])

被忽略。您还需要为您的字段提供 getter 和 setter,以便 DefaultModelBinder 可以设置 post 上的值(即使其成为 属性)。

public bool[] array { get; set; }

这是继 Stephen 和 haim770 解决方案之后的答案。谢谢

  public class StringNumber
{
    [Required]

    public bool[] array { get; set; }
    public double Numerito { get; set; }
    public double numero(bool[] array){
        int i = 0;
        double result=0.0;
        double temp=0.0;
        foreach (bool str in array){
            if( str == true)
                temp=1.0;
            else
                temp=0.0;
            result+= temp* System.Math.Pow(2,(double)i);
            i++;
        }
    return result;
    }
}