Hi dear ,
This blog I am writing code how show data using webgrid and edit.
Controller page:-
public class WebGridEditController : Controller
{
//
// GET:
/WebGridEdit/
BookRepoNewMvcEntities dt = new BookRepoNewMvcEntities();
public ActionResult Display()
{
var dis = dt.MyBooks.ToList();
return View(dis);
}
public ActionResult Edit(int id = 0)
{
MyBook book = dt.MyBooks.Find(id);
if (book == null)
{
return HttpNotFound();
}
return View(id);
}
[HttpPost]
public ActionResult Edit(MyBook bookit)
{
if (ModelState.IsValid)
{
dt.Entry(bookit).State =
System.Data.EntityState.Modified;
dt.SaveChanges();
return RedirectToAction("Show");
}
return View(bookit);
}
}
}
View page code:-
@model
IEnumerable<TESTMVC.Models.MyBook>
@{
ViewBag.Title = "Display";
}
<h2>Display</h2>
<html>
<head>
<title></title>
<style type="text/css">
body
{
background-color: #e295fc;
color: #1A1A1A;
font-size: .85em;
font-family: "Segoe UI", Verdana, Helvetica, Sans-Serif;
margin: 1.5em;
padding: 0;
}
.PGrid
{
margin: 2%;
border-collapse: collapse;
width: 95%;
}
.Header
{
font-weight: bold;
background-color: #60acf6;
color: #ff6a00;
text-decoration: none;
}
.PGrid th, .PGrid td
{
border: 1px solid #0094ff;
padding: 5px;
}
.altRow
{
color:aqua;
background-color:silver;
/*color:
brown;*/
/*background-color:
#808080;*/
}
</style>
</head>
<body>
@{
var grid = new WebGrid(Model, canPage: true, canSort: true, rowsPerPage: 3);
grid.Pager(WebGridPagerModes.NextPrevious);
@grid.GetHtml(tableStyle:"PGrid" ,headerStyle:"Header",alternatingRowStyle:"altRow",
columns: grid.Columns(
grid.Column("Id","Id", canSort:true),
grid.Column("Name","Name",canSort:true),
grid.Column("Author","Author"
,canSort:true),
grid.Column(header:"Edit",
format: @<text><a href="@Url.Action("Edit","WebGridEdit", new{id =item.id})"><img src="~/Images/edit_it.jpg" width="20" height="20" alt="" /></a></text>)
));
}
</body>
</html>
Edit
page code:-
@model
TESTMVC.Models.MyBook
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>MyBook</legend>
@Html.HiddenFor(model => model.Id)
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Author)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Author)
@Html.ValidationMessageFor(model => model.Author)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back
to List", "Index")
</div>
@section
Scripts {
@Scripts.Render("~/bundles/jqueryval")
}