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
arrayfield a property