【问题标题】:Checkboxes array of bools doesnt brings the true value if it's checked by user如果用户检查了复选框,则布尔数组不会带来真实值
【发布时间】:2015-08-18 14:35:08
【问题描述】:

我有一个 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 = "索引"; } 灵感来自: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”。

提前致谢

【问题讨论】:

  • 将您的 array 字段设为属性
  • 不是已经完成了吗?公共布尔 [] 数组 = 新布尔 [27]; public double numero(bool[] array){
  • 据我所知,没有。
  • 你说得对,现在我知道你在说什么了,谢谢

标签: asp.net-mvc controller asp.net-mvc-5 views


【解决方案1】:

删除您为同一属性创建的隐藏输入

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

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

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

被忽略。您还需要提供您的字段获取器和设置器,以便DefaultModelBinder 可以在回发时设置值(即使其成为属性)。

public bool[] array { get; set; }

【讨论】:

    【解决方案2】:

    这是 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-04
      • 1970-01-01
      • 2013-12-09
      • 2015-04-13
      • 1970-01-01
      • 2015-01-09
      • 2014-01-24
      • 2017-10-14
      • 1970-01-01
      相关资源
      最近更新 更多