I have a model
public class SexModel
{
public SexModel()
{
this.Man = "Man";
this.Woman = "Woman";
this.ManId = 1;
this.WomanId = 2;
this.WomanSelected = this.ManSelected = false;
}
public bool ManSelected { get; set; }
public bool WomanSelected { get; set; }
public string Man { get; set; }
public string Woman { get; set; }
public int ManId { get; set; }
public int WomanId { get; set; }
}
create a radio button on my view
@Html.RadioButton(Model.Man, Model.ManId, Model.ManIsSelected,
new { @id = Model.ManId})
@Html.RadioButton(Model.Man, Model.WomanId, Model.WomanSelected,
new { @id = Model.WomanId })
user can select man or woman radio buttons on register form, but why always WomanSelected and ManSelected are both false after click submit form button in my action?
You should be binding your radio buttons in MVC via RadioButtonFor i.e.
@Html.RadioButtonFor(m => m.ManSelected, m.Man);
@Html.RadioButtonFor(m => m.WomanSelected, m.Woman);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With