find a location for property in a new city

Saturday, 13 August 2011

ASP.NET MVC Checkbox has a hidden input too

I noticed strange behaviour whilst coding and ASP.NET MVC CheckBox Html Helper. I noticed that the field which was in part of a GET form was creating the query string with the same field in it twice. When the box was checked one parameter was true and the other was false.

Say I have a boolean field, MyField, that I am creating a checkbox for. I would write this: @Html.CheckBox("MyField"). You would expect this to output a single check box but if you actually look at the HTML that is generated you will notice that is also creates a hidden field.



I found out the the reason for this is written within the ASP.NET MVC source code:
if (inputType == InputType.CheckBox) {
    // Render an additional  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();
}

I appreciate the reasoning behind this but in my scenario MyField is part of a ViewModel that is being sent with the form and of course boolean values are False by default so this is wasted on my scenario.

Another reason I do not like this is because I am using the GET method on my form the user will see this oddity in the querystring. Worse still, they be a developer and judge my code as rubbish not knowing it is the doing of ASP.NET MVC. I can't have that! ;)

Solution

Simple solution really. Write the HTML you want in HTML, forget about the HtmlHelper:
<input type="checkbox" name="MyField" value="true" id="MyField"
@Html.Raw((Model.MyField) ? "checked=\"checked\"" : "") />

Remember the value="true" because the default value to be sent is "on" if it is checked, which obviously can't be parsed to a Boolean.

Obviously, this doesn't look that clean but I will continue to do this on forms that use the GET method.

Follow britishdev on Twitter

24 comments:

  1. You, sir, just helped me out of 3-hrs nightmare hunt. Thank you!

    ReplyDelete
    Replies
    1. I am writing this comment with tears of joy. My marriage fell apart after 6 months because my mother In-law asked my husband to divorce me and marriage the woman she betroth to him as his wife. All this drama started happening in our marriage and my husband left me and our one month baby just so he could do as his mom wants him to.I was in tears and shattered for 6 months because I could not imagine my whole life crumbling in front of me. I could not continue with work and baby responsibilities so I quit working. Things became more difficult until my best friend advised me to meet a love doctor that fixed her marriage with a RETURN LOVER SPELL that works as fast as 12 hours after casting the spell. I concord and decided to contact this spell doctor and guess what. My husband came to my mom house with his family in less than 12 hours after casting the spell. Even his mom came back asking me for forgiveness and also to reconcile with my husband who left me. They have been coming for the past 3 days and now everyone is asking me to take him back. I am so grateful for what this love spell doctor has done for me. I am thinking of accepting his apologize and move on as family just as my mom advised.Anyone who wants their lover back should write to love doctor on Love Spell doctor on lovespelldoctor0@gmail.com God bless you as you find your happiness through this testimony.

      Delete
  2. Thanks, was pulling my hair out over this!

    ReplyDelete
  3. Thank you! Thank you! Thank you! This was spot on and solved my problem

    ReplyDelete
    Replies
    1. Love spell doctor has brought my husband back to me after 6 months of separation. My husband left me and wanted a divorce because of a young girl who is young enough to be her daughter. Your love spell has reunited my family once again after mourning and tears that I am now a single mom, but my teas has turn to Joy with your amazing return lover spell.
      I am grateful for everything. Anyone experiencing heartbreak and needs their lover back should contact lovespelldoctor0@gmail.com and that will be the end of your problems in that relationship.
















      Love spell doctor has brought my husband back to me after 6 months of separation. My husband left me and wanted a divorce because of a young girl who is young enough to be her daughter. Your love spell has reunited my family once again after mourning and tears that I am now a single mom, but my teas has turn to Joy with your amazing return lover spell.
      I am grateful for everything. Anyone experiencing heartbreak and needs their lover back should contact lovespelldoctor0@gmail.com and that will be the end of your problems in that relationship.





















      Love spell doctor has brought my husband back to me after 6 months of separation. My husband left me and wanted a divorce because of a young girl who is young enough to be her daughter. Your love spell has reunited my family once again after mourning and tears that I am now a single mom, but my teas has turn to Joy with your amazing return lover spell.
      I am grateful for everything. Anyone experiencing heartbreak and needs their lover back should contact lovespelldoctor0@gmail.com and that will be the end of your problems in that relationship.

      Delete
  4. Many Many thanks. Now my problem is resolved.
    I was wasting 2-3 hr every night since 1 week and now my issue is resolved. It's really great.

    ReplyDelete
  5. Motifz Designer Lawn. 1, 2 & 3 Piece Unstitched Premium Embroidered Lawn 2019, Premium embroidered Lawn, Premium lawn, Premium lawn 2019, Motifz, Premium lawn in Pakistan. Shipping worldwide. Stitching option available.

    ReplyDelete
  6. Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!Thanks this person tried to unlock your phone

    ReplyDelete
  7. It should be noted that whilst ordering papers for sale at paper writing service, you can get unkind attitude. In case you feel that the bureau is trying to cheat you, don't buy term paper from it. Wordpress Tips

    ReplyDelete
  8. I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article. สมัครเน็ตทรู

    ReplyDelete
  9. Acquiring Custom Assignment Writing Services from a credible firm like Legitimate Assignment Writing Company ensures that you attain excellence, especially in your College Assignment Writing Services endeavors, which can be challenging in many instances.

    ReplyDelete
  10. That definitely possibly an amazing organize i always in truth definitely relished checking out. It's not necessarily specifically frequent i always add some choice to identify a selected matter. microsoft office 365 product key

    ReplyDelete
  11. Thanks for this article very helpful. thanks. Adele net worth

    ReplyDelete
  12. Great article Lot's of information to Read...Great Man Keep Posting and update to People..Thanks Adele net worth

    ReplyDelete
  13. Snappy disclaimer: I AM NOT BASHING ANYONE! Indeed, I was yelling that. This segment of the article is mentioning to you what I know, read, hear and involvement with gaming.
    Subway surfer hack

    ReplyDelete
  14. Love spell doctor has brought my husband back to me after 6 months of separation. My husband left me and wanted a divorce because of a young girl who is young enough to be her daughter. Your love spell has reunited my family once again after mourning and tears that I am now a single mom, but my teas has turn to Joy with your amazing return lover spell.
    I am grateful for everything. Anyone experiencing heartbreak and needs their lover back should contact lovespelldoctor0@gmail.com and that will be the end of your problems in that relationship.

    ReplyDelete
  15. I never believed that i will be happy again after much frustration. I have been rejected by my husband after Six(6) years of marriage just because he met another woman and he left me and the kids to suffer. One day when i was reading through the web i stumbled at a post on how chief dr lucky the great spell caster has helped people to restore broken marriages and relationships and curing of diseases. I emailed him immediately to help my situation via: chiefdrlucky@gmail.com He told me not to worry that my husband will return to me begging and shedding tears of forgiveness. I am so happy that my husband actually returned to me in 48 hours after i obeyed the words and instructions of chief dr lucky. contact him today in any of your problems for a lasting solution via: chiefdrlucky@gmail.com or call/whats app +2348132777335 Website: http://chiefdrluckysolutionhome.website2.me/  

    ReplyDelete
  16. Very efficiently written information. It will be beneficial to anybody who utilizes it, including me. Keep up the good work. For sure i will check out more posts. This site seems to get a good amount of visitors. hidden marriage

    ReplyDelete
  17. i will love to share my testimony to you all the people in world i got married to my husband about 2 year ago we start having problems at home like we stop sleeping on the same bed,fighting about little things he always comes home late at night,drinking too much and sleeping with other women out side i have never love any man in my life except him. he is the father of my child and i don't want to loose him because we have worked so hard together to become what we are and have today .few month ago he now decided to live me and the kid,being a single mother can be hard sometimes and so i have nobody to turn to and i was heart broken.i called my mom and explain every thing to her,my mother told me about DR.kuta how he helped her solve the problem between her and my dad i was surprise about it because they have been without each other for three and a half years and it was like a miracle how they came back to each other. i was directed to DR. kuta on his email. (drkutaherbalcenter@gmail.com) and explain everything to him,so he promise me not to worry that he will cast a spell and make things come back to how we where so much in love again and that it was another female spirit that was controlling my husband he told me that my problem will be solved within two days if i believe i said OK So he cast a spell for me and after two days my love came back asking me to forgive him i Am so happy now. so that why i decided to share my experience with every body that have such problem contact Dr kuta the great spell caster on his email addresses (drkutaherbalcenter@gmail.com)or whatsapp Dr kuta on (+2347054547814)

    ReplyDelete