MVC Razor Boolean EditorTemplate With Bootstrap Formatting (Updated)

In my post – MVC Razor Boolean Formatting – I talked about using Editor Templates to globally use Bootstrap boolean controls in your MVC application. With the Bootstrap v3 release, that code has become obsolete. So, in the interests of correctness, I thought it necessary to show updated code.

@model bool?

    @{
        // make use of MVC4 nullable class attribute values
        var yesSelected = Model.HasValue && Model.Value ? "active btn-success" : null ;
        var noSelected = Model.HasValue && !Model.Value ? "active btn-success" : null;
        var noSelection = !Model.HasValue ? "active" : null;   
    
        // get the name of the ID - this is to support multiple fields     
        var htmlField = ViewData.TemplateInfo.HtmlFieldPrefix;
    }

    @Html.HiddenFor(model => model)

    <div class="btn-group [email protected]">
        <button type="button" class="btn btn-default @yesSelected [email protected]" 
            onclick="javascript:$('[email protected] button.active').removeClass('active btn-success');$('#@htmlField').val(true);$(this).addClass('active btn-success');">Yes</button>
        <button type="button" class="btn btn-default @noSelected [email protected]" 
            onclick="javascript:$('[email protected] button.active').removeClass('active btn-success');$('#@htmlField').val(false);$(this).addClass('active btn-success');">No</button>
    
        @if (ViewData.ModelMetadata.IsNullableValueType)
            { <button type="button" class="btn btn-info @noSelection [email protected]" onclick="javascript:$('#@htmlField').val('');" >Do Not Set</button> }

    </div>

Til next time…


24 Comments

  • cook carrots says:

    You can find reasons why carrots lay unmarked in the dinner-table and
    in the lunchbox. Some children say they are dull,
    plain, and humdrum. But all of that is just an issue of opinion.
    Do your best to liven up carrots in child- friendly manners
    and provide them to your kids frequently.
    These two keys to success are in improving the diet of your family your
    best bets. Child-pleasant carrots are how to cook carrots that is simple.
    Here are three ways carrots could be served to your own brood that ensure empty plates and abdomens full of nutritious carrots:

  • Travel agencies that are online are very popular among individuals over-all that and the world are why the demand of these on-line travel
    companies is so high. But you will find lots of Pros and cons of reserving an apartment through an internet
    travel company. Sometimes reserving a 30 through an online travel
    company may be very hectic and time consuming. Also, there are
    times when you may not get the best results.
    The Holidays bring smiles plus it fixes life. The lives of people around the world are extremely feverish and individuals live in an age of fast-track life style
    and that is the reason why it is very crucial that you refresh your life along with the best way to do it’s
    a vacation. There are places that are different all around
    the globe that are well-known for its vacation locations and these
    locations have become popular throughout the globe. Indonesia is the
    type of areas that are extremely popular amongst other people at
    all times the earth and the tourists.

  • sofa beds says:

    It is necessary that you know when trying to find the sofa bed auckland the best way to try to
    find the best discounts and bargains you want. This article is going to
    help you with that. Maintain in mind what you’re planning to study, and make sure so which
    you can know what to do, you pay attention.

  • It’s awesome for me to have a site, which is
    beneficial designed for my know-how. thanks admin

  • I’m curious to find out what blog system you have been using?

    I’m experiencing some small security problems with my latest website and I’d like to
    find something more safe. Do you have any recommendations?

  • I enjoy, cause I discovered exactly what I used to be looking for.
    You’ve ended my 4 day long hunt! God Bless you man. Have a nice day.
    Bye

  • Everythijng is very open with a very clear explanatiokn of thhe issues.
    It was definitely informative. Your site iis extremly helpful.
    Thank yyou for sharing!

  • web design says:

    Wonderful goods from you, man. I’ve understand your stuff previous to and you’re just too magnificent.
    I actually like what you have acquired here, really like what you’re
    saying and the way in which you say it. You make it entertaining and you still care
    for to keep it sensible. I can’t wait to read much more from you.

    This is really a wonderful web site.

  • fantastic issues altogether, you simply won a brand new
    reader. What might you suggest about your put up that you made a few
    days in the past? Any sure?

  • Amado says:

    Hello! I could have sworn I’ve visited this web site before but after
    going through some of the posts I realized it’s new
    to me. Nonetheless, I’m certainly happy I came across it and I’ll be bookmarking it and checking back
    often!

  • cyberfuc says:

    Thanks for any other fantastic article. The place else may anybody get that kind of info in such
    a perfect way of writing? I’ve a presentation subsequent week, and I’m on the look for such info.

  • Nice answers in return of this issue with solid arguments and telling the whole thing concerning that.

  • Why visitors still use to read news papers when in this technological world everything is presented on web?

  • It’s in reality a great and helpful piece of information. I’m happy that
    you shared this useful information with us.
    Please stay us informed like this. Thank you for
    sharing.

  • blah says:

    Spot on with this write-up, I absolutely think this amazing site needs far more attention. I’ll probably be back again to read through more, thanks for the information!

  • Judy says:

    I will promote your website or link to pr9 social sites for
    usa visitors traffic..

  • pekka says:

    I know this web site presents quality dependent articles and other stuff,
    is there any other web site which presents such data in quality?

  • Amazing things here. I’m very happy to peer your post.
    Thanks so much and I’m taking a look forward to contact you.

    Will you please drop me a e-mail?

  • Argent Crystal was born to make superior sanitary products for satisfying folks’s quality residing with
    more complete product lines, which steadily increase and prolong from initial rest room mirrors
    and bathroom accessories to toilet furniture and bathe programs.

  • I’m now not sure the place you’re getting your information, but
    great topic. I needs to spend a while finding out more or figuring out
    more. Thanks for great info I was on the lookout for this information for my mission.

  • There is certainly a lot to find out about this topic.
    I like all the points you have made.

  • I have been exploring for a little for any Һigh-quality articles
    or webⅼog posts іn this kind of space . Explorіng in Yahoo I finally stumbled ᥙρon this աebsite.
    Studying this info So i’m happy to conveү tһat I’ve an incredibly ǥood uncanny feeling I ϲame սpon just what I needed.
    I so much no doubt will maҝe certain to do not forget this web site and рrovides it a glance regulɑrly.

  • porn says:

    ᕼello there! I sіmply աish to offer you a big thumbs up
    for youг excellent information you have right here on this pоst.
    I ɑm coming back to youг web site for more soon.

  • After looking іnto a few of the articles on your Ьlog,
    I really appreciate youг way of blоgging. I bookmarked it to my bookmаrk
    webpage list and will be checқing baсk soon. Take a look at my website aѕ well ɑnd tell me
    your opinion.

Leave a Reply

Your email address will not be published. Required fields are marked *