0

I'm using the below given table in Partial view and I want to set the value of checkboxes to true by default using Html Helper. I have tried

<input type="checkbox" value="@Model[i].IsSelected" checked="checked" />

but it passes the false value to controller even if I checked it to true. Here is my table code. .

@model List<SchoolManagment.Models.tbl_Student>

<table id="tableforposting" class="table table-hover table-bordered">
    <thead>
        <tr>
            <th>Sr#</th>
            <th class="col-sm-1"></th>
            <th>STUDENT NAME</th>
            <th>ROLL NO.</th>
        </tr>
    </thead>
    <tbody>
            @for (int i = 0; i < Model.Count(); i++)
            {
                <tr>
                    <td class="col-sm-1 text-center">
                        @Html.CheckBoxFor(x => x[i].IsSelected)
                    </td>
                    <td>
                        @Html.DisplayFor(x => x[i].Name)
                    </td>
                    <td>
                        @Html.DisplayFor(x => x[i].Roll_No)
                        @Html.HiddenFor(x => x[i].Roll_No)
                    </td>
                </tr>
            }
    </tbody>
</table>
ekad
  • 14,436
  • 26
  • 44
  • 46
Zain Ali
  • 81
  • 9

1 Answers1

0

In Controller action, you can set the Checkbox model property true to create the checked checkbox.

public ActionResult Index(Register model)
{
    model.Terms = true;
    return View(model);
}

In View,

@Html.CheckBoxFor(m => m.Terms)

For reference, How to set a CheckBox by default Checked in ASp.Net MVC

Parag
  • 38
  • 8