Search code examples
c#asp.net-mvc-3webforms-view-engine

How can I add a checkbox in mvc3?


  1. I want to know that how to add the checkbox in the mvc3 asp.net C#. I'm using Aspx view engine.

  2. I have to add mutiple checkboxes and have to save the data of that checkbox which are selected true.

How can i do this?


Solution

  • Here is an example:

    In your "Model class":

    public class TennisCourt
    {
        [Key]
        public int ID { get; set; }
    
        [Display(Name = "Extérieur ?")]//=Outside in french
        [Column("Outside")]
        public bool Outside { get; set; }
    }
    

    In your "View Index"

    @model IEnumerable<TennisOnline.Models.TennisCourt>
    
    @{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>Gestion des Courts</h2>
    
    <p>
    @Html.ActionLink("Create New", "Create")
    </p>
    <table>
    <tr>
        <th>
         Id    
        </th>
        <th>
            Outside ?
        </th>
    
        <th></th>
    </tr>
    
    @foreach (var item in Model) {
    <tr>
        <td>
           @Html.DisplayFor(modelItem => item.ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Outside)
        </td>
    
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
    }
    

    Here is the result:

    enter image description here