Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The question is explained in details here i want to perform edit and delete operation on webgrid without using Entity framework in mvc4 i am using LinqtoSql, i want to do it as we use to do it in asp.net in gridview, that when we click edit in gridview a textbox would appear inside gridview but the same isnt possible in webgrid in mvc pls some one having done that can pls help me on how to achive that?
and i have to update my database based on the changes pls help me on that!!!

Model class contains this code:
C#
namespace MvcApplication10.Models
{
    public class Display
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Mobile { get; set; }
        public string Address { get; set;}
        public string City { get; set; }


    }

}


the controller code is as shown below:

C#
namespace MvcApplication10.Controllers
{
    public class DisplayController : Controller
    {
        //
        // GET: /Display/
        TestdisplayDataContext td = new TestdisplayDataContext();

        public ActionResult Testdisplay()
        {
            List<Display> displays = new List<Display>();

            using (TestdisplayDataContext td = new TestdisplayDataContext())
            {
                var que = (from c in td.Dsiaplytests 
                           select new Display {Id=c.Id,Name=c.Name,Mobile=c.Mobile,Address=c.Address,City=c.City }).ToList();
                displays = que;
            }

            return View(displays);
        }

        private Display GetId(int Id)
        {
            Display display = null;

            using (TestdisplayDataContext td = new TestdisplayDataContext())
            {
                var que = (from c in td.Dsiaplytests where c.Id == Convert.ToInt32(Id) select c).ToList();

                if (que != null)
                {
                    display = que.ToList();

                }
            }

            return display;
        }

        public ActionResult Edit(int id)
        {

            return View();
        }

    }
}


and the cshtml code is as shown bellow:

Razor
@{
    ViewBag.Title = "Testdisplay";
    Layout = "~/Views/Shared/_Layout.cshtml";

    var grid = new WebGrid(source: Model, canPage: true, rowsPerPage: 3);
}


<style type="text/css">
    /*Here we will add css for style webgrid*/
    .webgrid-table
    {
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 1.2em;
        width: 100%;
        display: table;
        border-collapse: separate;
        border: solid 1px #000000;
        background-color: white;
    }

        .webgrid-table td, th
        {
            border: 1px solid #6f6a6a;
            padding: 3px 7px 2px;
        }
        .webgrid-table th a:link
        {
            color:white;
        }

    .webgrid-header
    {
        background-color:#6f6a6a;
        color: #FFFFFF;
        padding-bottom: 4px;
        padding-top: 5px;
        text-align: left;
    }

    .webgrid-footer
    {
    }

    .webgrid-row-style
    {
        padding: 3px 7px 2px;
    }

    .webgrid-alternating-row
    {
        background-color: #dddddd;
        padding: 3px 7px 2px;
    }
</style>

<div id="content">  
    @grid.GetHtml(  
     tableStyle: "webgrid-table",  
     headerStyle: "webgrid-header",  
     footerStyle: "webgrid-footer",  
     alternatingRowStyle: "webgrid-alternating-row",  
     rowStyle: "webgrid-row-style",  
     columns: grid.Columns  
     (grid.Column(columnName:"Id",header: "Id"),  
     grid.Column(columnName:"Name", header: "Name"),  
     grid.Column(columnName:"Mobile", header: "Mobile"),  
     grid.Column(columnName:"Address", header: "Address"),  
     grid.Column(columnName:"City", header:"City"),
    grid.Column(header: "View", format: (item) => Html.ActionLink("View", "View", "Home", new { id = item.Id }, null)),   
    grid.Column(header: "Edit", format: (item) => Html.ActionLink("Edit", "Edit", "Home", new { id = item.Id }, null)),  
    grid.Column(header: "Delete", format: (item) => Html.ActionLink("Delete", "Delete", "Home", new { id = item.Id }, null))
    )  
    )  
</div>  
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900