This project is read-only.

Unknown problem with CheckBoxListFor control

Feb 26, 2011 at 8:54 PM
Edited Feb 26, 2011 at 9:17 PM

Hi

First great work!

 

EDIT: i succeeded in isolating the problem, its the name of the check boxes when i do not use the template then it render with the names fieldname.Items[0].Selected but when i use the

template then the name is fieldname.$.a fieldname.$.b and so one ... (x.X) ehm what do i use in the template CheckBox name ?

 

the template

<%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl"%>
<%@ Import Namespace="MVCControlsToolkit.Core" %>
<%@ Import Namespace=" MVCControlsToolkit.Controls" %>

<div class="radioGroup">
<% For Each item As CheckBoxListItem(Of String) In Model.items%>
    <p><%: Html.CheckBox(item.Code, item.Selected, New With {.class = "validateGroupCheckBox"})%> <%: Html.Label(item.Label)%></p>
<% Next%>
</div>

 

I have a problem making me nuts, im using the CheckBoxListFor helper (in my strongly typed view) with one of my classes

 

Public Class myclass
    Public Property value() As String
    Public Property text() As String
End Class

the implementation of the check box list is: 

 

<%: Html.CheckBoxListFor(Function(m) m.fieldname, New ChoiceList(Of myclass, String, String)(Model.getlist(), (Function(t) t.value), (Function(t) t.text)), True, "myCustomListTemplate")%>

 

 

im creating the list to use with

 

Public Function getlist() As List(Of myclass)
            Return New List(Of myclass)() From { _
               New myclass() With { _
                .value = "a", _
                .text = "a" _
               }, _
               New myclass() With { _
                .value = "b", _
                .text = "b" _
               }, _
               New myclass() With { _
                .value = "c", _
                .text = "c" _
               },
               New myclass() With { _
                .value = "d", _
                .text = "d" _
               },
               New myclass() With { _
                .value = "e", _
                .text = "e" _
               },
               New myclass() With { _
                .value = "f", _
                .text = "f" _
               },
               New myclass() With { _
                .value = "g", _
                .text = "g" _
               },
               New myclass() With { _
                .value = "h", _
                .text = "h" _
               },
               New myclass() With { _
                .value = "i", _
                .text = "i" _
               } _
              }
        End Function

 

 

the list template who i think is the problem here because when i render the list without it then everything works. When i debug using the template then the check boxes never saves its state its always not checked.

 

 


Coordinator
Feb 28, 2011 at 10:14 AM

Hi thanks for your feed back,

I will review your code and I will correct the bug for the 0.9 version of the Mvc Controls toolkit.

pls submit further bugs in the Issue Tracker section, so they will be available in the task tracking system! (don't need to move this one...just next time) 

Feb 28, 2011 at 1:02 PM
Ok will do, sorry if i put it on the wrong topic its my first time posting on codeplex Ler
From: [email removed]
Sent: Monday, February 28, 2011 11:14 AM
To: [email removed]
Subject: Re: Unknown problem with CheckBoxListFor control [MVCControlsToolkit:247708]

From: frankabbruzzese

Hi thanks for your feed back,

I will review your code and I will correct the bug for the 0.9 version of the Mvc Controls toolkit.

pls submit further bugs in the Issue Tracker section, so they will be available in the task tracking system! (don't need to move this one...just next time)

Coordinator
Feb 28, 2011 at 1:23 PM

Would you, please, be so kind to post also the code of your template? I need to be sure that there is no "name prefix error".  Have you used CheckBox or CheckBoxFor helpers in your template or a custom checkbox?

Because if you use CheckBox or CheckBoxFor they compute automatically  the right prefix to add to the name of the checkbox, so you need to give the checkbox just the name of the boolean property you want to use,

otherwise you have to compute yourself the full name of the input field by using the helper: 

Html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName("NameOfTheBooleanProperty")

Feb 28, 2011 at 1:40 PM
Edited Feb 28, 2011 at 3:43 PM
i used Html.CheckBox with the name set to item.Code
Each item.code was named like:
SomeName
SomeOtherName
In the name it existed the following chars äåö who outputed with one char of the following _ for each char named ä å ö when rendered
<%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl"%>
<%@ Import Namespace="MVCControlsToolkit.Core" %>
<%@ Import Namespace=" MVCControlsToolkit.Controls" %>
<% For Each item As CheckBoxListItem(Of String) In Model.items%>
<%: Html.CheckBox(item.Code, item.Selected, New With {.class = "validateGroupCheckBoxes"})%> <%: Html.Label(item.Label)%>

 

Here is the list i use and its data

Public Function H�mtaTj�nster() As List(Of tj�nster)
            Return New List(Of tj�nster)() From { _
               New tj�nster() With { _
                .value = "kontorsst�dning", _
                .text = "kontorsst�dning" _
               }, _
               New tj�nster() With { _
                .value = "industrist�dning", _
                .text = "industrist�dning" _
               }, _
               New tj�nster() With { _
                .value = "butiksst�dning", _
                .text = "butiksst�dning" _
               },
               New tj�nster() With { _
                .value = "f�nsterputs", _
                .text = "f�nsterputs" _
               },
               New tj�nster() With { _
                .value = "trappst�dning", _
                .text = "trappst�dning" _
               },
               New tj�nster() With { _
                .value = "skoladagisst�dning", _
                .text = "skola & dagis st�dning" _
               },
               New tj�nster() With { _
                .value = "hemst�dning", _
                .text = "hemst�dning" _
               },
               New tj�nster() With { _
                .value = "storst�dning", _
                .text = "storst�dning" _
               },
               New tj�nster() With { _
                .value = "flyttst�dning", _
                .text = "flyttst�dning" _
               } _
              }
        End Function

Coordinator
Feb 28, 2011 at 9:51 PM

The way you used the template is wrong. However, the bug is in the documentation where I forgot to explain how to build the template. Since I like your example, I will be glad to you if you send me

a selfcontained project containing your example list. If you send it to me I will correct the template and then I will put your example in the documentation page here: http://mvccontrolstoolkit.codeplex.com/wikipage?title=CheckBox%20list%20for%20selecting%20elements

You can send me everything through the contact page of my blog:  http://www.dotnet-programming.com/contact.aspx

The contact page allow the use of an attachment so you can put your project into a .zip file

Coordinator
Mar 6, 2011 at 1:51 PM

I have updated the documentation to explain better what datatype to use to build the strongly typed View for the template. However, since the use of a single template for all the items is "difficult", in the 0.9 release I give also option

to use a template to be used for each single item, and in the case of the DataGrid. See the updated documentation here: http://mvccontrolstoolkit.codeplex.com/wikipage?title=CheckBox%20list%20for%20selecting%20elements