Oct 1 2009

ASP.Net MVC 1.0 and Html.CheckBox

Category: amilsark @ 18:40

With a background in ASP.Net web forms, the Html.CheckBox used with ASP.Net MVC did not behave how I thought it would.

For example when adding a checkbox to the View inside a table:

   1: <%=Html.CheckBox("cb" + item.player.idPlayer)%>

Firebug shows me this rendered in the html source of the page:

image


What is that hidden field for???


A developer on the Microsoft ASP.Net team said that this was by design so that we can properly assign a value of ‘false’ to an unchecked checkbox (link). 

I’m assuming this was the fix for the issue in the MVC 1.0 RC builds where a null value was being returning for unchecked checkboxes.


But in ASP.Net MVC 1.0, here is what the Request.Form values are (notice all checkbox inputs are listed with either a value of ‘false’ or ‘true’):

image

 


So in my case to loop through the returned inputs and grab the Player ID’s which have been checked:

Here is the controller action code to look for checked checkboxes (from Andrey Tkach on Stack Overflow):




   1: var selectedPlayerIds = new List<int>();
   2:             IEnumerable<Player> players;
   3:  
   4:             foreach (string key in Request.Form)
   5:             {
   6:                 var checkBox = String.Empty;
   7:                 if(key.StartsWith("cb"))
   8:                 {
   9:                     checkBox = Request.Form["" + key];
  10:                     if (checkBox != "false")
  11:                     {
  12:                         int id = Convert.ToInt32(key.Remove(0, 2));
  13:                         selectedPlayerIds.Add(id);
  14:                     }
  15:                 }
  16:             }





I am now able to use these playerID’s for interaction with the repository and perform interesting logic.

Tags: , ,

Comments

1.
Abe Rusell United States says:

Hi, I can’t understand how to add your site in my rss reader. Can you Help me, please  <a href="hride.com/.../">credit rewards cards</a>

2.
Flashy Amy United States says:

This sucks my wii just died. my bad if I'm a little out there I just need to get it off my chest.

3.
Sash Windows United States says:

It's crazy this gets written about so little, I'm sure the demand is there.

4.
Online Consignment Shop United States says:

Have you ever thought about adding a little bit more than just your thoughts?  I mean, what you say is important and everything.  But its got no punch, no pop!  Maybe if you added a pic or two, a video?  You could have such a more powerful blog if you let people SEE what youre talking about instead of just reading it.

5.
Corey Weinzierl United States says:

I really enjoyed reading this blog. Nice choice of color. Keep the articles coming.

6.
Ellis Hospkins United States says:

Excellent post!   <a href=”http://www.ebulletproofvest.com”>bulletproof clothing</a>

7.
Jordan Shoes United States says:

I'm lovin' this blog. Lots of very interesting info.

8.
Janiece Willhite United States says:

It is great to see how your success has developed and the long road you’ve taken. My sincerest wish is to be in a similar situation, one day. As Keisha already said (and I believe this, too) … you, your blog and your thoughts have been very inspiring to many people. Thanks so much!

9.
How to save a marriage United States says:

Hey... Things look a little screwy with the with the layout of the page. For some reason the text block is running into the margins. I don't know if it's just me or have you heard this from other ppl?  Just wanted to let you know in case you've been in the past couple of days. Thanks! How to save a marriage

10.
Dorsey Prem United States says:

Ahhh the Old Sox.  Great info here.  So sad they are this injured this year.

11.
Background Check Company United States says:

Technically, blogs consider up really little space - all you require for blogs is the script for example Wordpress, Cutenews, FanUpdate, etc. which are small sized files. Unless you upload loads of huge pictures which will eventually hog up a great deal of the space. Social Networkings obviously consider up a lot of space assuming that individuals would be able to upload their pictures, videos, etc.

12.
registry cleaners United States says:

Your current blogs usually include many of really up to date info.  Where do you come up with this?  Just saying you are very resourceful.  Thanks again

13.
PC Health Advisor United States says:

I hope you never stop!  This is one of the best blogs Ive ever read.  Youve got some mad skill here, man.  I just hope that you dont lose your style because youre definitely one of the coolest bloggers out there.  Please keep it up because the internet needs someone like you spreading the word.

14.
Air Jordan 4 United States says:

I like this blog. The only thing I do not like is that I didn't find it earlier.

15.
PC Health Advisor United States says:

I was very ecstatic to find this site on bing.I wished to say thanks to you with regard to this excellent article!! I certainlyenjoyed every little bit of it and I've you bookmarked to check out new stuff you post.

16.
registry cleaners United States says:

You...are...my...hero!!!  I cant believe something like this exists on the internet!  Its so true, so honest, and more than that you dont sound like an idiot!  Finally, someone who knows how to talk about a subject without sounding like a kid who didnt get that bike he wanted for Christmas.

17.
Cursos de ingles en el extranjero United States says:

I like that you've touched on this subject, it's somewhat of a rariety.

18.
Porter Cable Battery United States says:

Blog Engine FTW! andrewmilsark.com | ASP.Net MVC 1.0 and Html.CheckBox is cool.  I am really interested and was wondering if anyone else had any other related posts they could suggest. I like writing squidoo lenses myself and would like to gather as much data as I can. Please take a look one of my little projects if you like

20.
Pozycjonowanie Warszawa United States says:

I fully agree with author opinion.

21.
cadillac United States says:

Thanks for writing this I'll tell my partner and see what she thinks.

22.
Online Consignment Shop United States says:

Can I just say what a relief to find someone who actually knows what theyre talking about on the internet. You definitely know how to bring an issue to light and make it important. More people need to read this and understand this side of the story. I cant believe youre not more popular because you definitely have the gift.

23.
Noma Brotzman United States says:

This was a great post and I look forward to many other great stories from you and your site.

24.
Orlando Randleman United States says:

Zune and iPod: Most people compare the Zune to the Touch, but after seeing how slim and surprisingly small and light it is, I consider it to be a rather unique hybrid that combines qualities of both the Touch and the Nano. It's very colorful and lovely OLED screen is slightly smaller than the touch screen, but the player itself feels quite a bit smaller and lighter. It weighs about 2/3 as much, and is noticeably smaller in width and height, while being just a hair thicker.

25.
Li Cambel United States says:

Thanks for writing this blog and sharing it with the world. I would like to know how to go for reading your rss blog. Please let me know if possible.

26.
garmin 255w United States says:

I have been reading your posts during my afternoon break, and I have to admit the whole article has been very informative and very well composed. I thought I would let you know that for some reason this blog does not display well in IE 8. I wish Microsoft would stop changing their software. I have a question for you. Do you mind swapping blog roll links? That would be really great!

27.
cadillac escalade United States says:

Thanks for writing this I'll tell my partner and see what she thinks.

28.
oak brook discount broker United States says:

This is a really informative post. Thank you for sharing it with us.

30.
honolulu multiple listing United States says:

I just read through the entire article of yours and it was quite good. This is a great article thanks for sharing this informative information. I will visit your blog regularly for some latest post.

31.
help you sell United States says:

Great, I have already bookmarked your this page...Now I don’t have enough time for read but by reading beginning part I must say...it was a positive start .. Would love to read further too...Thanks for great post!

32.
chicago for sale by owner United States says:

This is a great blog. Thanks for all your hard work and the info you give.

33.
Luke Croke United States says:

Very Interesting! I just found your blog on google and bookmarked it. Have a nice day

34.
chat nude United States says:

I have been checking out your posts for the last couple of hours, and everything has been very informative and well written.  I just wanted to tell you that for some reason this one doesn't seem to work in Internet Explorer.  On a side note, I was wondering if you wanted to swap blogroll links?  I hope to hear from you soon!

35.
Save Marriage Alone United States says:

Helpful resource... thanks for this. I will browse your  entire site and come back sometime.

36.
branding agency United States says:

Nice! I'm waiting for your sharings thank you.

37.
Towing brooklyn park MN United States says:

This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion. You also know how to make people rally behind it, obviously from the responses. You have got a design here that’s not too flashy, but makes a statement as big as what you are saying. Great job, indeed.

38.
Towing Boston MA United States says:

Very helpful blog.. Thank you for the information...

39.
Electric Cigarettes United States says:

I dont know what to say. This web site is amazing. Thats not seriously a genuinely massive statement, but its all I could appear up with right after reading this. You know a great deal about this theme. So much so that you manufactured me need to understand more about this. Your web site is my stepping stone, my close friend. Many thanks for the heads up on this subject matter.

40.
Avery Calvary United States says:

Hey, great blog you have here, think I came across it on Yahoo but i'm not sure now...anyway, I'll check back again!

41.
Alejandro Miler United States says:

nice blog... why don't you post more?

42.
odzież używana United States says:

Jamorama i think you have a great site here... today was my first time coming here.. i just happened to find it doing a google search. anyway, great post.. i'll be bookmarking this page for sure.

43.
Genevieve Fretwell Taiwan says:

Hi just stumbled your blog and have been browsing around some of your posts and just wondering why you chose a Microsoft blog dont you find it impossible to do anything with?

44.
Lucienne Charle United States says:

do you have an rss feed? I want to add it to my reader but I can't find it...

45.
austin Turkey says:

Excellent blog! I actually love how its easy on my eyes as well as the facts are well written. I am wondering how I could be notified whenever a new post has been made. I have subscribed to your rss feed which really should do the trick! Have a nice day!

46.
Chris Nauss United States says:

Reading all of the good reviews of your blog, I thought I would also join in and let you know that I really enjoyed reading your blog. There are some weird comments, but generally I agree with what the other rewiewers are saying.

47.
Reuben Clopton United States says:

are you using wp for this blog?

48.
Destination Wedding United States says:

Hello,I love reading through your blog, I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts.

49.
Giant Mountain Bike United States says:

The best bikes are giant mountain bikes, , it's so much fun.

50.
student driver magnetic sign United States says:

Have you thought about including some social bookmarking buttons to these blog posts. At least  for facebook.

51.
Giant Mountain Bike United States says:

Alot to consider there, relative pitch training is still one of the best to musical mastery.

52.
Cam Girls United States says:

Hello,I love reading through your blog, I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts.

53.
bail baltimore United States says:

What are you stating, man?  I know everyones got their own view, but really?  Listen, your web log is neat.  I like the effort you put into it, particularly with the vids and the pics.  But, come on.  Theres gotta be a better way to say this, a way that doesnt make it seem like everyone here is stupid!

54.
Nude Cam Girls United States says:

Why didn’t I find this post earlier? Keep up the good work!

55.
Destination Wedding United States says:

Hi. this is kind of an -unconventional- question , but have other visitors asked you how get the menu bar to look like you've got it? I also have a blog and am really looking to alter around the theme, however am scared to death to mess with it for fear of the search engines punishing me. I am very new to all of this ...so i am just not positive exactly how to try to to it all yet. I'll just keep working on it one day at a time.

56.
garmin 255w United States says:

Thank you for making the sincere effort to divulge this. I feel fairly strong about it and would like to read more. If it's possible, as you gain more in depth knowledge, would you mind adding more articles similar to this one with more information? It would be very helpful and useful for me and my friends.

57.
Electric Cigarettes United States says:

Intriguing piece of writing, where did you acquire the tips about your post?

58.
gamefly free trial United States says:

This is an awesome post, im super glad that i stumble upon it. maybe i will even submit this to stumble upon, i think my group would enjoy it.

59.
gamefly free 30 day trial United States says:

This is an awesome post, im super glad that i stumble upon it. maybe i will even submit this to stumble upon, i think my group would enjoy it.

60.
blonde jokes United States says:

nice blog! keep the good work!

61.
best quit smoking products United States says:

You have to be warned that you will experience some withdrawal pains from nicotine. But not to worry, there are lots of nicotine patches and gums out there that will help you get through this problem. That will take care of the biological addiction, but you will still have to deal with the psychological addiction which is the biggest addiction of all.

62.
Nude Cam United States says:

I cannot seem to locate your syndication feed, I would like to get more your posts.

63.
accutane lawsuit United States says:

Your blog design is awesome. is this a template or did your programer custom design this for you?

64.
accutane lawsuit United States says:

Your blog design is awesome. is this a template or did your programer custom design this for you?

65.
irving plumber United States says:

I'm honestly not to familiar with this subject. you made it seem pretty interesting though. i really only surfed your blog to get some ideas for the blog im getting ready to launch, but besides your design i enjoyed your wrtings too

66.
irving plumber United States says:

I'm honestly not to familiar with this subject. you made it seem pretty interesting though. i really only surfed your blog to get some ideas for the blog im getting ready to launch, but besides your design i enjoyed your wrtings too

67.
little tikes guitar United States says:

This is a great gift for my sister. Awesome christmas present

68.
Walker Bienkowski United States says:

this was a very entertaining read.  i enjoyed it very much!

69.
Baby Gifts, New Baby Gifts United States says:

You...are...my...hero!!!  I cant believe something like this exists on the internet!  Its so true, so honest, and more than that you dont sound like an idiot!  Finally, someone who knows how to talk about a subject without sounding like a kid who didnt get that bike he wanted for Christmas.

70.
Thomas Benfield United States says:

i'm adding your blog rss feed so that i can see your new posts.  keep up the good work!

71.
victorian overmantle mirror United States says:

Valuable information and excellent layout you got here! I would just like to thank you for sharing your thoughts and also time in the stuff you post!! Thumbs up

72.
victorian overmantle mirror United States says:

Considerably, the article is in reality the greatest on this precious topic. I harmonise with your conclusions and will eagerly look forward to your next updates. Simply just saying thanks can not simply be enough, for the phenomenal lucidity in your writing. I will directly grab your rss feed to stay informed of any updates. De lightful work and much success in your business dealings!

73.
Janot United States says:

Merely want to say your article is stunning. The clearness in your post is simply impressive and i can take for granted you are an expert on this subject. Well with your permission allow me to grab your rss feed to keep up to date with incoming post. Thanks a million and please keep up the respectable work.

74.
gift ideas United States says:

try to change your template, there are some great ones...

75.
garmin 255w gps United States says:

Hi. I wanted to write you a quick comment to express my thanks. I've been following your blog for a month or so and have picked up a ton of beneficial information as well as enjoyed the way you've setup your blog. I am attempting to run my own blog but I think it's far too general. I would like to focus more on more specific topics. Being all things to all people is not all that its cracked up to be. Many thanks.

76.
Popular Designer Handbags United States says:

If you are dressing for any occasion, wearing a beautiful dress is not enough. You must bring a bag to complement your outfit worthy. Fashion Popular Designer Handbags are an important tool for all women in the fashion and Clutch is one of the most popular of this type. With every day new models of handbags on the market are increasing. may Recently, Lulu Guinness launched the pocket lips be a perfect gift for Valentine¡¯s Day? These bags are S. Day lip on it available in many beautiful colors.

77.
Karaoke Hire Brisbane United States says:

Awesome business! A mate told me about your site, he also writes these..

78.
David Printing United States says:

I'd love to use your last sentence as a quote for a poster.

79.
dating tips United States says:

how old is this post?

80.
nude chat United States says:

You're a very smart person!

81.
cheap nike sportswear United States says:

What doesn't kill us makes us stronger.

82.
subaru impreza wrx United States says:

In most cases, the dealer who leased the car to you will be responsible to accept it when the term of the lease is over. They will want to inspect the vehicle for damages, and close the contract.It is not their responsibility to call you, and, if you do not hear from them, you should make every attempt to contact them. The penalties for a late return can be severe, and simply failing to return a leased vehicle because you have not heard from anyone, will not save you from these penalties......Good luck to you.

83.
ETFs United States says:

do you have a different blog where you post more often?

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading