0

I have a list of Checkboxes in a bool array, I wish I could give the Checked value as a "true" in that specific checkbox if it's checked by user

This is my model

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

My controller 2 actions: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using 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);
            }
        }
    }

and the view: @model testnumbers.Models.StringNumber @{ ViewBag.Title = "Index"; } Inspired in: 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"  />
    }

The view alwys got thr values of the bool array as false and I would like to update to "true" those checkboxes that were selected to the controller and can do the work.

Thanks in advance

4
  • Make your array field a property Commented Jun 4, 2015 at 6:06
  • Isn't that done? public bool[] array = new bool[27]; public double numero(bool[] array){ Commented Jun 4, 2015 at 12:57
  • As far as I can see, no. Commented Jun 4, 2015 at 12:59
  • You are right, now I know what you where saying, Thanks Commented Jun 5, 2015 at 1:24

2 Answers 2

1

Remove the hidden input your creating for the same property

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

The DefaultModelBinder reads the first name/value pair match your property name and binds it. Any subsequent name/value pairs are ignored so the value of the inputs created by

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

are ignored. You also need to give your field getters and setters so the DefaultModelBinder can set the values on post back (i.e make it a property).

public bool[] array { get; set; }
Sign up to request clarification or add additional context in comments.

Comments

0

This is the answer following the Stephen and haim770 Solutions. Thank you

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.