0

The value of Online,Tele and Physical in DB with int datatype. In Edit View i want to checked the radiobutton which have the value 1 in db. The AppointCat is random property in model. I used this long if else condition in my Edit View. How i improve my code or is there any other way to write this code:

   @if (Model.ApOnline == 1)

     {
      <span>
      Online: @Html.RadioButtonFor(x => x.AppointCat, "Online", new { @checked = true })
                                        &nbsp; &nbsp;
       </span>
       <span>
       TeleMed: @Html.RadioButtonFor(x => x.AppointCat, "TeleMed")
                                        &nbsp; &nbsp;
       </span>
       <span>
       Physical: @Html.RadioButtonFor(x => x.AppointCat, "Physical")
       </span>

       }
       else if (Model.ApTele == 1)
       {
       <span>
       Online: @Html.RadioButtonFor(x => x.AppointCat, "Online")
            &nbsp; &nbsp;
       </span>
       <span>
       TeleMed: @Html.RadioButtonFor(x => x.AppointCat, "TeleMed", new { @checked = true })
                                   &nbsp; &nbsp;
        </span>
        <span>
                                        Physical: @Html.RadioButtonFor(x => x.AppointCat, "Physical")
                                    </span>
                                }
                                else
                                {
                                    <span>
                                        Online: @Html.RadioButtonFor(x => x.AppointCat, "Online")
                                        &nbsp; &nbsp;
                                    </span>
                                    <span>
                                        TeleMed: @Html.RadioButtonFor(x => x.AppointCat, "TeleMed")
                                        &nbsp; &nbsp;
                                    </span>
                                    <span>
                                        Physical: @Html.RadioButtonFor(x => x.AppointCat, "Physical", new { @checked = true })
                                    </span>
                                }

You can understand from my code that i am noob in MVC. Thanks for the help in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Burair abbas
  • 77
  • 1
  • 7
  • You don't need the `if/else`. This `Html.RadioButtonFor(x => x.AppointCat, "Online")` means *create a radio button for `AppointCat` and if the value of the `AppointCat` is `Online`, then make this button selected.* So you need to create one of those for each value `AppointCat` can have. – CodingYoshi Apr 24 '21 at 17:41
  • Thanks for your advice, but i put condition like this. Online: @Html.RadioButtonFor(x => x.AppointCat, "Online", (Model.ApOnline == 1) ? new { @checked = true } : null) – Burair abbas Apr 29 '21 at 01:02
  • That's fine but it's redundant. This is enough: `Html.RadioButtonFor(x => x.AppointCat, "Online")` and does that for you. – CodingYoshi Apr 29 '21 at 11:59
  • I used it but its not working – Burair abbas Apr 29 '21 at 13:15

0 Answers0