Hi mukesh1,
I have created the example using the below article.
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