Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6804071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:25:17+00:00 2026-05-26T19:25:17+00:00

I have a seemingly easy task of setting a radio button checked based on

  • 0

I have a seemingly easy task of setting a radio button “checked” based on model’s boolean value. I have a boolean in my model “IsSDPDonor” which I want to use it for Yes/No Radio buttons. The “Yes” radio button should be checked if “IsSDPDonor” is true and “No” radio button when it is false. I tried to use the code below but it always checks the “No” radio button.

 @Html.Label("Would You Like to Donate Platelets (SDP)") : 
 @Html.RadioButtonFor(m => m.Donor.IsSDPDonor, true, new {@checked = Model.Donor.IsSDPDonor ? "checked" : ""}) Yes 
 @Html.RadioButtonFor(m => m.Donor.IsSDPDonor, false, new { @checked = !Model.Donor.IsSDPDonor ? "checked" : "" }) No

I was getting a little frustrated, so I thought that I would rather take some help. Basically, the HTML syntax says to put only the “Checked” attribute without any values to check a radio button. I was wondering how would I do that using MVC3 razor syntax.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:25:17+00:00Added an answer on May 26, 2026 at 7:25 pm

    Create the HTML options in a code block, leaving them null if the button shouldn’t be checked, then use the proper variable in your helper.

    @{
        var yesOptions = Model.Donor.IsSDPDonor ? new { @checked = "checked" } : null;
        var noOptions =  Model.Donor.IsSDPDonor ? null : new { @checked = "checked" };
    }
    @Html.Label("Would You Like to Donate Platelets (SDP)") : 
    @Html.RadioButtonFor(m => m.Donor.IsSDPDonor, true, yesOptions ) Yes 
    @Html.RadioButtonFor(m => m.Donor.IsSDPDonor, false, noOptions ) No
    

    EDIT: After looking at the source code, it appears to me that it should be setting this property based on the value of the model. Have you tried omitting the HTML attributes completely?

    case InputType.Radio:
        if (!usedModelState) {
            string modelStateValue = htmlHelper.GetModelStateValue(name, typeof(string)) as string;
            if (modelStateValue != null) {
                isChecked = String.Equals(modelStateValue, valueParameter, StringComparison.Ordinal);
                usedModelState = true;
            }
        }
        if (!usedModelState && useViewData) {
            isChecked = htmlHelper.EvalBoolean(name);
        }
        if (isChecked) {
            tagBuilder.MergeAttribute("checked", "checked");
        }
        tagBuilder.MergeAttribute("value", valueParameter, isExplicitValue);
        break;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been going crazy over a seemingly easy question with Python: I want
I thought the following task would be seemingly easy to code, but I have
I have a seemingly innocent code snippet, which I typed into Visual Studio 2008
I'm using Entity Framework 4.1 and have a seemingly simple requirement: I want to
I've been banging my head about this seemingly easy task and I could really
I have this seemingly easy issue in FF4. In the code below I need
Howdy, codeboys and codegirls! I have came across a simple problem with seemingly easy
I have a seemingly simple problem though i am unable to get my head
I have a seemingly simple problem whereby I wish to reconcile two lists so
I have no idea. This causes seemingly random time-outs. These in turn break the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.