asp.net mvc - Checkboxes array of bools doesnt brings the true value if it's checked by user -
i have list of checkboxes in bool array, wish give checked value "true" in specific checkbox if it's checked user
this 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 = 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, not save, return current umbraco page if (modelstate.isvalid == false) { redirecttoaction("index"); } number.numerito = number.numero(number.array); return view(number); } } }
and view: @model testnumbers.models.stringnumber @{ viewbag.title = "index"; } inspired in: this post 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 = 0; <table> <tr> @for (i = 0; < 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 bool array false , update "true" checkboxes selected controller , can work.
thanks in advance
remove hidden input creating same property
@html.hiddenfor(m => m.array[i])
the defaultmodelbinder
reads first name/value pair match property name , binds it. subsequent name/value pairs ignored value of inputs created by
@html.checkboxfor(m => m.array[i])
are ignored. need give field getters , setters defaultmodelbinder
can set values on post (i.e make property).
public bool[] array { get; set; }
Comments
Post a Comment