Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Razor RadioButton ViewModel Property Binding

Razor RadioButton ViewModel Property Binding

Scheduled Pinned Locked Moved ASP.NET
htmlasp-netwpfwcfsysadmin
2 Posts 2 Posters 14 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    j11codep
    wrote on last edited by
    #1

    Hi, I'm having a hard time finding a simple enough (ELI5) direct answer to on here and the interwebz If I have the following html fragment on a razor page. How does the clicked button bind to the viewmodel Gender property (transfer data to/from viewmodel property and radiobutton input element)? If I understand it correctly, this binding happens server-side after a POST. This fragment below would have been inside a form, so ALL (male, female in this case) the radio button input types will get sent together with the hidden element. The expression parameters is what tells server-side to associate the radio buttons to the viewmodel property. Also, doesn't the expression already tell what to bind the radiobutton to (ie the hiddenfor is not necessary)?

    @model Student

    @Html.RadioButtonFor(m => m.Gender,"Male")
    @Html.RadioButtonFor(m => m.Gender,"Female")

    @Html.HiddenFor(model => model.Gender)

    Richard DeemingR 1 Reply Last reply
    0
    • J j11codep

      Hi, I'm having a hard time finding a simple enough (ELI5) direct answer to on here and the interwebz If I have the following html fragment on a razor page. How does the clicked button bind to the viewmodel Gender property (transfer data to/from viewmodel property and radiobutton input element)? If I understand it correctly, this binding happens server-side after a POST. This fragment below would have been inside a form, so ALL (male, female in this case) the radio button input types will get sent together with the hidden element. The expression parameters is what tells server-side to associate the radio buttons to the viewmodel property. Also, doesn't the expression already tell what to bind the radiobutton to (ie the hiddenfor is not necessary)?

      @model Student

      @Html.RadioButtonFor(m => m.Gender,"Male")
      @Html.RadioButtonFor(m => m.Gender,"Female")

      @Html.HiddenFor(model => model.Gender)

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      RadioButtonFor renders an <input type="radio"> with the name set to the property name, and the value set to the specified value. If the model property matches the specified value, then the radio button will be checked. <input type="radio"> - HTML: HyperText Markup Language | MDN[^] When the form is submitted, the name and value of the checked radio button (if any) will be sent to the server, and the model binder will use it to populate the property. Adding the extra HiddenFor for the same property should not be necessary.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups