Sunday 15 February 2015

Delete multiple selected checkbox from webgrid using mvc 4


Delete  multiple selected checkbox using  mvc 4:-
Model:-
public class delcheck
    {
        [Required(ErrorMessage="Enter a valid Id")]
        public int StuId { get; set; }
        [Required(ErrorMessage="Enter a valid name")]
        public string StuName { get; set; }
        [Required(ErrorMessage=" Enter a course name")]
        public string StuCourse { get; set; }
        [Required(ErrorMessage="Enter a valid cityname")]
        public string StuCity { get; set; }
    } public class delcheck
    {
        [Required(ErrorMessage="Enter a valid Id")]
        public int StuId { get; set; }
        [Required(ErrorMessage="Enter a valid name")]
        public string StuName { get; set; }
        [Required(ErrorMessage=" Enter a course name")]
        public string StuCourse { get; set; }
        [Required(ErrorMessage="Enter a valid cityname")]
        public string StuCity { get; set; }
    }
Controller:-
public class mycheckdelController : Controller
    {
        //
        // GET: /mycheckdel/
        deldatamvc2015Entities dt = new deldatamvc2015Entities();
        public ActionResult Index()
        {
            List<Student> mydata = new List<Student>();
            mydata = dt.Students.ToList();
            return View(mydata);
        }
        public ActionResult Delete(string[] ids)
        {
            int[] id = null;
            if (ids != null)
            {
                id = new int[ids.Length];
                int j = 0;
                foreach (string i in ids)
                {
                    int.TryParse(i, out id[j++]);
                }

            }
            if (id != null && id.Length > 0)
            {
                List<Student> allselect = new List<Student>();
                using (deldatamvc2015Entities dc = new deldatamvc2015Entities())
                {
                    allselect = dc.Students.Where(a => id.Contains(a.StuId)).ToList();
                    foreach (var i in allselect)
                    {
                        dc.Students.Remove(i);
                    }
                    dc.SaveChanges();
                }
            }
            return RedirectToAction("Index");
        }
    }
View page:-
@model IEnumerable<mvcmvc2015.Student>
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<html>
    <head>
        <title>WELCOME</title>
    </head>
    <body>
        @using (Html.BeginForm("Delete", "mycheckdel", FormMethod.Post))
        {
            var grid = new WebGrid(Model, canSort: true, canPage: true);
            grid.Pager(WebGridPagerModes.NextPrevious);
            @grid.GetHtml(columns:grid.Columns(
            grid.Column("StuId","StuId", canSort:true),
            grid.Column("StuName","StuName", canSort:true),
            grid.Column("StuCourse","StuCourse" ,canSort:true),
            grid.Column("StuCity","StuCity", canSort:true),
            grid.Column(header:"Select",format:@<text><input type="checkbox" name="ids" value="@item.StuId" /></text>)
                     ));
            <input type="submit" name="Delete It" />
        }
    </body>
</html>
View For delete:-
@model mvcmvc2015.Models.delcheck

@{
    ViewBag.Title = "Delete";
}

<h2>Delete</h2>

<h3>Are you sure you want to delete this?</h3>
<fieldset>
    <legend>delcheck</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.StuId)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.StuId)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.StuName)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.StuName)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.StuCourse)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.StuCourse)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.StuCity)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.StuCity)
    </div>
</fieldset>
@using (Html.BeginForm()) {
    <p>
        <input type="submit" value="Delete" /> |
        @Html.ActionLink("Back to List", "Index")
    </p>
}