Edit and Update record in HTML Table using Entity Framework in ASP.Net MVC

mukesh1
 
on Aug 06, 2018 01:50 AM
Sample_118766.zip
6014 Views

If i have 150 columns in database, i want to update only 50,then How i can update in 50 only,without change in others columns,means no need to specify values for others column here.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 06, 2018 07:14 AM

Hi mukesh1,

I have created the example using the below article.

Editable Table MVC: Implement Insert Update Edit Delete in HTML Table using ASP.Net MVC

Check this example. Now please take its reference and correct your code.

Controller

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        TestEntities entities = new TestEntities();
        List<Customer> customers = entities.Customers.ToList();
        return View(customers);
    }

    [HttpGet]
    public ActionResult Edit(int? id)
    {
        TestEntities entities = new TestEntities();
        Customer updatedCustomer = (from c in entities.Customers
                                    where c.CustomerId == id
                                    select c).FirstOrDefault();
        return View(updatedCustomer);
    }

    [HttpPost]
    public ActionResult Edit(Customer customer)
    {
        using (TestEntities entities = new TestEntities())
        {
            Customer updatedCustomer = (from c in entities.Customers
                                        where c.CustomerId == customer.CustomerId
                                        select c).FirstOrDefault();
            updatedCustomer.Name = customer.Name;
            updatedCustomer.Country = customer.Country;
            entities.SaveChanges();
        }

        return RedirectToAction("Index", "Home");
    }
}

View

Index

@model IEnumerable<_MVC_CRUD_EntityFramework.Customer>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Country)
            </th>
            <th></th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Country)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.CustomerId })
                </td>
            </tr>
        }
    </table>
</body>
</html>

Edit

@model _MVC_CRUD_EntityFramework.Customer

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Edit</title>
</head>
<body>
    @using (Html.BeginForm())
    {
        <table class="table">
            <tr>
                <td>
                    @Html.HiddenFor(model => model.CustomerId)
                    @Html.DisplayFor(model => model.Name)
                </td>
                <td>
                    @Html.EditorFor(model => model.Name)
                </td>
            </tr>
            <tr>
                <td>
                    @Html.DisplayNameFor(model => model.Country)
                </td>
                <td>
                    @Html.EditorFor(model => model.Country)
                </td>
            </tr>
        </table>
        <input type="submit" value="Update" class="btn btn-default" />
    }
</body>
</html>

Screenshot