c# - How to bind checkbox values to a list of ints? -
I'm following this example
but when I submit I say, then it says
'wrong' can not be changed as 'wrong'
Because it believes in the wrong or true that it is being passed, Not a real value
What am I doing wrong?
My model is
Public IEnumerable & lt; AllocateRequirementViewModel & gt; Requirements {Get; Set; } Public listing & lt; Int & gt; Get the requirements {receive; Set; }
Then my razor is
& lt; Div id = "RequirementsContainer" & gt; @Vrench (Requirements in Model Requirements) {& lt; Div class = "row" & gt; & Lt; Input id = "@ requirement.ID" type = "checkbox" name = "requirement iids" value = "@ requirement.id" /> @ requirement. Description & lt; / Div & gt; } & Lt; / Div & gt;
Your model will need to include checked values and all possible values . Public class testvindell {public guide id (get; set;} public idictionary & lt; guid, string & gt; allCheckboxOptions {get; set;} public guide [] checked Options {get; set;}} I use guides in my database as PK, so these are databases from the ID
I then make html Created multiple extension methods for
your extension methodpublic static MvcHtmlString CheckBoxList (The HtmlHelper HtmlHelper, string name,
listInfo, IDictionary & lt; string, object & gt; htmlAttributes, Int32 column) from the list {if (String.IsNullOrEmpty (name)) throw new ArgumentException ("logic ("ListInfo"); if (listInfo.Any ()) returns the new MvcHtmlString (String.Empty); Var external Sb = new string builder (); (inter 32 i = 0; i so you can call in keeping
@ Html.CheckBoxList ("yourtypes", (from Model.AllCheckboxOptions) O New selection SelectListItem {text = o.Value, selected = Model.CheckedOptions.Contains (o.Key), value = o.Key.ToString ()}). ToList ())
I have used this technique with both MVC3 and MVC4 with success.
Comments
Post a Comment