Radio buttons for 3 boolean properties

I have a model with 3 properties:

public class OptionsViewModel {
  public Boolean A { get; set; }
  public Boolean B { get; set; }
  public Boolean C { get; set; }
}

I need to display 3 radio buttons for 3 properties included in one group.

So I added the following

@Html.RadioButtonFor(x => x.A)
@Html.RadioButtonFor(x => x.B)
@Html.RadioButtonFor(x => x.C)

I have your boolean properties, not just one ...

The code does not compile, because RadioButtonFor does not have such a constructor.

I tried several options, but in the end I can choose a lot of switches or the binding does not work ...

Can someone tell me the best way to solve this problem with my model?

+4
source share
4 answers

Have you tried this?

@Html.RadioButtonFor(x => x.A, "A", true) A
@Html.RadioButtonFor(x => x.B, "B", true) B
@Html.RadioButtonFor(x => x.C, "C", false) C

More details here http://www.c-sharpcorner.com/uploadfile/b19d5a/how-to-create-radio-button-in-asp-net-mvc3-razor-application/

0
source
0

3 3 , .

:

public class OptionsViewModel {
  public bool A { get; set; }
  public bool B { get; set; }
  public bool C { get; set; }
  public bool Value { get; set; }
}

:

@Html.RadioButtonFor(x => x.Value, "A", model.A) A
@Html.RadioButtonFor(x => x.Value, "B", model.B) B
@Html.RadioButtonFor(x => x.Value, "C", model.C) C

RadioButton 1 .

0

:

:

@Html.RadioButtonFor(model => model.A, "A", new { @Name = "sign", @checked = Model.A })
@Html.RadioButtonFor(model => model.B, "B", new { @Name = "sign", @checked = Model.B })
@Html.RadioButtonFor(model => model.C, "C", new { @Name = "sign", @checked = Model.C })

:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Add(ModelType dataItem, FormCollection formValues)
{
   dataItem.A = formValues["sign"] == "A";
   dataItem.B = formValues["sign"] == "B";
   dataItem.C = formValues["sign"] == "C";
   ...
}
0

All Articles