Thomas
Thomas

Reputation: 34188

Checkbox is not getting checked inside webgrid

When I run this code then all CheckBoxes are showing unchecked.

@{
    var grid = new WebGrid(Model);
}

<div id="gridContent" style=" padding:20px; ">
    @grid.GetHtml(
        tableStyle: "webgrid-table",
        headerStyle: "webgrid-header",
        footerStyle: "webgrid-footer",
        alternatingRowStyle: "webgrid-alternating-row",
        selectedRowStyle: "webgrid-selected-row",
        rowStyle: "webgrid-row-style",
        mode: WebGridPagerModes.All,
        columns:
            grid.Columns(
            grid.Column(columnName: "ID", header: "ID", format: @<text>@item.ID</text>),
            grid.Column(columnName: "FirstName", header: "First Name", format: @<text>@item.FirstName</text> ),
            grid.Column(columnName: "LastName", header: "Last Name", format: @<text>@item.LastName</text>),
            grid.Column(header: "IsActive",
            format: @<text><input id="select" class="box" name="select" type="checkbox" value="@item.IsActive" /></text>,
            style: "text-center checkbox-width")
            ))
</div>

IsActive is always having true as value. Model class looks like:

public class Student
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool IsActive { get; set; }

    public IList<Student> GetStudents()
    {
        string connectionStringName = System.Configuration.ConfigurationManager.ConnectionStrings["StudentDBContext"].ConnectionString;
        IList<Student> _Student = new List<Student>();

        using (SqlConnection connection = new SqlConnection(connectionStringName))
        {
            SqlCommand command = new SqlCommand(
              "SELECT ID, FirstName,LastName,IsActive FROM Student;", connection);

            connection.Open();

            SqlDataReader reader = command.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    _Student.Add(new Student()
                    {
                        ID = Convert.ToInt32(reader["ID"].ToString()),
                        FirstName = reader["FirstName"].ToString(),
                        LastName = reader["LastName"].ToString(),
                        IsActive = Convert.ToBoolean(reader["IsActive"])
                    });
                }
            }
            reader.Close();
        }

        return _Student;
    }

    public Student()
    {
    }
}

Just do not understand where I made the mistake.

Upvotes: 0

Views: 1447

Answers (1)

Bruno Pessanha
Bruno Pessanha

Reputation: 3024

Instead of:

<input id="select" class="box" name="select" type="checkbox" value="@item.IsActive" />

You should do:

<input id="select" class="box" name="select" type="checkbox" @(item.IsActive ? "checked='checked'" : "") value="@item.IsActive" />

More information about how to check a checkbox here.

Upvotes: 1

Related Questions