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

  • SEARCH
  • Home
  • 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 7646363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:11:19+00:00 2026-05-31T10:11:19+00:00

Currently i am creating two buttons and setting the selected value this way: @if

  • 0

Currently i am creating two buttons and setting the selected value this way:

@if (Model.Test_Emne == null)
{
    @Html.RadioButtonFor(x => x.Test_Emne, 1) <span>Ja</span>
    <br />
    @Html.RadioButtonFor(x => x.Test_Emne, 0) <span>Nej</span>

}

@if (Model.Test_Emne == true)
{
    @Html.RadioButtonFor(x => x.Test_Emne, 1, new { @checked = "checked" }) <span>Ja</span>
    <br />
    @Html.RadioButtonFor(x => x.Test_Emne, 0) <span>Nej</span>
} else if (Model.Test_Emne == false)
{
    @Html.RadioButtonFor(x => x.Test_Emne, 1) <span>Ja</span>
    <br />
    @Html.RadioButtonFor(x => x.Test_Emne, 0, new { @checked = "checked" }) <span>Nej</span>
}

I have a lot of radio buttons on my page, so i am looking for a way to do this with less code.

I have also seen:

Has anyone implement RadioButtonListFor<T> for ASP.NET MVC?

and
http://jonlanceley.blogspot.com/2011/06/mvc3-radiobuttonlist-helper.html

Are these the only options?

  • 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-31T10:11:20+00:00Added an answer on May 31, 2026 at 10:11 am

    The following seems shorter way to achieve the same:

    @Html.RadioButtonFor(x => x.Test_Emne, true) <span>Ja</span>
    <br />
    @Html.RadioButtonFor(x => x.Test_Emne, false) <span>Nej</span>
    

    Possible scenarios when rendering the view:

    • Test_Emne = null => none of the radios is checked
    • Test_Emne = true => the first radio is checked
    • Test_Emne = false => the second radio is checked

    Possible scenarios when posting back:

    • If none of the radios is checked the Test_Emne property will be set to null
    • If the first radio is checked the Test_Emne property will be set to true
    • If the second radio is checked the Test_Emne property will be set to false

    UPDATE:

    This could be extended to any property and any numbers of radio buttons. For example:

    public string Foo { get; set; }
    

    and then:

    @Html.RadioButtonFor(x => x.Foo, "value1") <span>Foo 1</span>
    @Html.RadioButtonFor(x => x.Foo, "value2") <span>Foo 2</span>
    @Html.RadioButtonFor(x => x.Foo, "value3") <span>Foo 3</span>
    @Html.RadioButtonFor(x => x.Foo, "value4") <span>Foo 4</span>
    ...
    

    and then depending of the value of the Foo property the corresponding radio button will be selected. For example if you set model.Foo = "value3"; the third radio will be preselected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently creating an explicit reference to this in the outer class so that
I'm creating a SMS fording app. I currently got two classes; a main Activity
So I currently have a jQuery dialog with two buttons: Save and Close. I
I'm currently creating an application for a customer that will allow them to automatically
I'm currently creating a WinForm in VB.NET bound to an access database. Basically what
I am currently creating an e-commerce site using C# ASP.NET MVC and have just
We are currently creating an InfoPath 2007 form is deployed in SharePoint 2007. In
I am currently creating a Perl module for in house use. I used ExtUtils::ModuleMaker
I'm currently creating a Flash CMS via Flex and the Zend Framework. In the
I am currently creating a type of classifieds website, and when the user enters

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.