if (inputType == InputType.CheckBox) {
// Render an additional <input type="hidden".../> for checkboxes. This
// addresses scenarios where unchecked checkboxes are not sent in the request.
// Sending a hidden input makes it possible to know that the checkbox was present
// on the page when the request was submitted.
StringBuilder inputItemBuilder = new StringBuilder();
inputItemBuilder.Append(tagBuilder.ToString(TagRenderMode.SelfClosing));
This is ridiculous. In the case where I am naming a series of checkboxes with teh same name this code ruins everything. I can't have an int array because "true" and "false" rubbish is polluting my form post. Furthermore the basis for including this is totally
flawed. How does having a bunch of "true" "false" "true" tell me anything. It doesn't. It doesn't tell me that id 56 wasn't checked. It tell me that form element with name "vresiteid" at index 5 (for example) was or wasn't checked... oh how unhelpful that
is. How could this not have been included as an option at least in the Html.Checkbox method??
This sort of fruity weird stuff is exactly what I hoped to leave behind by going from webforms to MVC.
And Avinash Tiwari... your reply has no bearing whatsoever on my question.
-- Sam Critchley
"Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT
I can't have an int array because "true" and "false" rubbish is polluting my form post.
Yes, Html.CheckBox is prepared for another scenarios. If you want to use check-boxes in this way then you have to write own extension method for check-box rendering and own model-binder that will be capable to handle more values under one key in POST
or GET data. It's pretty easy. If you want then I could write this for you...
Don't forget to click "Mark as Answer" on the post that helped you.
With a single input, the system cannot differentiate between "this checkbox was rendered but the user chose not to check it" and "this checkbox was never rendered". This is a very common solution, for the system to post back either two values ("true" and "false")
if it's checked, or one value (just "false") if it's not, and only look at the first value in the series.
Marked as answer by Eilon on Aug 18, 2009 04:43 PM
The Html.CheckBox() helper is explicitly for rendering single checkboxes, not checkbox lists. (Though to be fair, we probably haven't done a very good job at documenting this.) The MVC assemblies don't contain a helper for rendering checkbox lists, so
the only solution at the moment is to do it by hand.
Marked as answer by Eilon on Aug 18, 2009 04:43 PM
Ok, seems reasonable. I thought I was in the twilight zone there for a while; it was getting a bit frustrating. I'll just do it old school and write out my tags.
Thanks folks.
-- Sam Critchley
"Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT
But you will still have issues if you are hoping to get the value of this to come through to the action as a boolean parameter. I never did find a solution I really liked. However with a single checkbox the out of the box mvc helper shoudl work fine.
-- Sam Critchley
"Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT
worldspawn[]
Contributor
6081 Points
1336 Posts
Html.Checkbox does weird stuff
May 21, 2009 09:10 AM|LINK
Why does this:
<%= Html.CheckBox("vreSiteId", new { value = vreSite.VRESiteId })%>
output this:
What the smeg is this stupid hidden field for and how do i get rid of it??? It's preventing me from having the action i want:
public ActionResult Index(int[] vreSiteId)
"Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT
software development
qwe123kids
All-Star
48619 Points
7957 Posts
MVP
Re: Html.Checkbox does weird stuff
May 21, 2009 09:25 AM|LINK
<%=%>
above symbol Means :- response.write
what everthere is in Html.CheckBox("vreSiteId", new { value = vreSite.VRESiteId }) method will have same effect as response.write
Avinash Tiwari
Remember to click “Mark as Answer” on the post, if it helps you.
hiren.bakhai
Member
2 Points
1 Post
Re: Html.Checkbox does weird stuff
May 21, 2009 09:27 AM|LINK
try out this one
<
input type="checkbox" value="<%= Html.Encode(vreSite.VRESiteId)%>" name="vreSiteId" />worldspawn[]
Contributor
6081 Points
1336 Posts
Re: Html.Checkbox does weird stuff
May 21, 2009 09:30 AM|LINK
I found this in the MVC source:
if (inputType == InputType.CheckBox) {
// Render an additional <input type="hidden".../> for checkboxes. This
// addresses scenarios where unchecked checkboxes are not sent in the request.
// Sending a hidden input makes it possible to know that the checkbox was present
// on the page when the request was submitted.
StringBuilder inputItemBuilder = new StringBuilder();
inputItemBuilder.Append(tagBuilder.ToString(TagRenderMode.SelfClosing));
TagBuilder hiddenInput = new TagBuilder("input");
hiddenInput.MergeAttribute("type", HtmlHelper.GetInputTypeString(InputType.Hidden));
hiddenInput.MergeAttribute("name", name);
hiddenInput.MergeAttribute("value", "false");
inputItemBuilder.Append(hiddenInput.ToString(TagRenderMode.SelfClosing));
return inputItemBuilder.ToString();
}
This is ridiculous. In the case where I am naming a series of checkboxes with teh same name this code ruins everything. I can't have an int array because "true" and "false" rubbish is polluting my form post. Furthermore the basis for including this is totally flawed. How does having a bunch of "true" "false" "true" tell me anything. It doesn't. It doesn't tell me that id 56 wasn't checked. It tell me that form element with name "vresiteid" at index 5 (for example) was or wasn't checked... oh how unhelpful that is. How could this not have been included as an option at least in the Html.Checkbox method??
This sort of fruity weird stuff is exactly what I hoped to leave behind by going from webforms to MVC.
And Avinash Tiwari... your reply has no bearing whatsoever on my question.
"Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT
software development
Augi
Contributor
6730 Points
1142 Posts
Re: Html.Checkbox does weird stuff
May 21, 2009 01:22 PM|LINK
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: Html.Checkbox does weird stuff
May 21, 2009 03:13 PM|LINK
levib
Star
7702 Points
1099 Posts
Microsoft
Re: Html.Checkbox does weird stuff
May 21, 2009 11:18 PM|LINK
The Html.CheckBox() helper is explicitly for rendering single checkboxes, not checkbox lists. (Though to be fair, we probably haven't done a very good job at documenting this.) The MVC assemblies don't contain a helper for rendering checkbox lists, so the only solution at the moment is to do it by hand.
worldspawn[]
Contributor
6081 Points
1336 Posts
Re: Html.Checkbox does weird stuff
May 22, 2009 12:16 AM|LINK
Ok, seems reasonable. I thought I was in the twilight zone there for a while; it was getting a bit frustrating. I'll just do it old school and write out my tags.
Thanks folks.
"Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT
software development
imperialx
Member
523 Points
1220 Posts
Re: Html.Checkbox does weird stuff
Aug 16, 2009 01:39 PM|LINK
Hi WorldSpawn,
May I know how you managed to get it? I'm not getting what I want below using a single checkbox.
<%Html.CheckBox("checkTermAgreement", If(ViewData("checkagreement") = Nothing, True, False))%>
worldspawn[]
Contributor
6081 Points
1336 Posts
Re: Html.Checkbox does weird stuff
Aug 28, 2009 12:11 AM|LINK
Manually:
<input type="checkbox" name="x" />
But you will still have issues if you are hoping to get the value of this to come through to the action as a boolean parameter. I never did find a solution I really liked. However with a single checkbox the out of the box mvc helper shoudl work fine.
"Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT
software development