0

I have defined connection alternatives for email and SMS. I want to have email checked by default. I wondered if I can add attribute checked in Razor without changing model or controller. Because if I use plain HTML, then it works but with Razor and when I inspection the page, there is no checked attribute.

@checked = "checked"
@checked = checked
@checked = "true"
@checked = true
new{@checked = "checked"}

<div class="checkbox-wrapper">
                    <label>
                        @Html.CheckBoxFor(m => m.Email, new { @class = "form-check-input", id = "E-post", @checked = "checked" })
                        <label for="E-post">E-post</label>
                    </label>
                </div>

email checkbox checked when the page is loaded

iCode18
  • 9
  • 5

2 Answers2

0

I found that CheckBoxFor does not have an attribute checked. The solution is to use CheckBox and then the second attribute is the boolean true or false which is the answer to the attribute isChecked.

@Html.CheckBox("name",true,new {@class="className",id="idName"})
iCode18
  • 9
  • 5
0

In your controller action,set Model.Email=true;.

this link might help you, set a CheckBox by default Checked

Hiba T
  • 41
  • 8