Hi
I´m using MVC3 + Razor + Telerik and I have a Grid where the method Edit, update, Insert and Delete doesn´t work.
For example in Update after de Wiew layer the way is
1.- On the controllers
public ViewResult Index()
return View(db.SYS_UF.ToList());
2.- @{
Layout = "~/Views/Shared/_Layout.cshtml";
}
3.- Index.cshtml
@{
GridButtonType type = GridButtonType.Image;
GridEditMode mode = GridEditMode.InLine ;
}
@{
ViewBag.Title = "Lista de UFs";
}
...
... and On to build the screen
Have any bory some example ?
Thanks A lot.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using STC_WEB.Models;
using Telerik.Web.Mvc;
using STC_WEB.Controllers.ClassesGrid;
namespace STC_WEB.Controllers
{
public class UFController : Controller
{
private AGRELENSEEntities db = new AGRELENSEEntities();
/****************************************************************/
//Used for the Ajax binding
[GridAction]
public ActionResult _SelectAjaxEditing()
{
List<SYS_UF> lstP = UFGrid.All();
return View(new GridModel { Data = lstP });
//return View(new GridModel(PagamentoGrid.All()));
}
[AcceptVerbs(HttpVerbs.Post)]
[GridAction]
public ActionResult _InsertAjaxEditing(int id)
{
//Create a new instance of the EditableProduct class.
SYS_UF uf = new SYS_UF();
//Perform model binding (fill the product properties and validate it).
if (TryUpdateModel(uf))
{
uf.SYS_UF_CD = Convert.ToString(id);
UFGrid.Insert(uf);
}
List<SYS_UF> lstP = UFGrid.All();
return View(new GridModel { Data = lstP});
}
[AcceptVerbs(HttpVerbs.Post)]
[GridAction]
public ActionResult _SaveAjaxEditing(int id)
{
SYS_UF uf = UFGrid.One(p => Convert.ToInt32(p.SYS_UF_CD) == id);
TryUpdateModel(uf);
UFGrid.Update(uf);
List<SYS_UF> lstP = UFGrid.All();
return View(new GridModel { Data = lstP });
}
[AcceptVerbs(HttpVerbs.Post)]
[GridAction]
public ActionResult _DeleteAjaxEditing(int id)
{
//Find a customer with ProductID equal to the id action parameter
SYS_UF uf = UFGrid.One(p => Convert.ToInt32(p.SYS_UF_CD) == id);
if (uf != null)
{
//Delete the record
UFGrid.Delete(uf);
}
List<SYS_UF> lstP = UFGrid.All();
return View(new GridModel { Data = lstP });
}
/****************************************************************/
/****************************************************************/
// GET: /UF/
public ViewResult Index()
{
return View(db.SYS_UF.ToList());
}
// GET: /UF/Details/5
public ViewResult Details(string id)
{
SYS_UF sys_uf = (from uf in db.SYS_UF where uf.SYS_UF_CD == id select uf).First();
return View(sys_uf);
}
//
// GET: /UF/Create
public ActionResult Create()
{
return View();
}
// POST: /UF/Create
[HttpPost]
public ActionResult Create(SYS_UF sys_uf) // Create([Bind(Exclude="id")]SYS_UF sys_uf) - qdo houver chave Autoincrementada
{
try
{
if (!ModelState.IsValid)
return View();
db.SYS_UF.Add(sys_uf);
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View(sys_uf);
}
}
//
// GET: /UF/Edit/5
public ActionResult Edit(string id)
{
var sys_uf = (from uf in db.SYS_UF where uf.SYS_UF_CD == id select uf).First();
return View(sys_uf);
}
//
// POST: /UF/Edit/5
[HttpPost]
public ActionResult Edit(SYS_UF sys_uf)
{
try
{
//TODO: add update logic here
if (ModelState.IsValid)
{
db.Entry(sys_uf).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(sys_uf);
}
catch
{
return View();
}
}
//
// GET: /UF/Delete/5
public ActionResult Delete(string id)
{
var sys_uf = (from uf in db.SYS_UF where uf.SYS_UF_CD == id select uf).First();
return View(sys_uf);
}
//
// POST: /UF/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(string id)
{
SYS_UF sys_uf = db.SYS_UF.Find(id);
db.SYS_UF.Remove(sys_uf);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
@model STC_WEB.Models.SYS_UF
@{
ViewBag.Title = "Inclusão de uma nova UF";
}
-* Create.cshtml *-
<h2>Inserir nova UF</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend></legend>
<div class="editor-label">
@Html.LabelFor(model => model.SYS_UF_CD)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.SYS_UF_CD)
@Html.ValidationMessageFor(model => model.SYS_UF_CD)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.SYS_UF_DESC)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.SYS_UF_DESC)
@Html.ValidationMessageFor(model => model.SYS_UF_DESC)
</div>
<p>
<input type="submit" value="Incluir" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Voltar para a Lista", "Index")
</div>
@model STC_WEB.Models.SYS_UF
@{
ViewBag.Title = "Exclusão de UF";
}
-* Dele.cshtml *-
<h2>Exclusão</h2>
<h3>Você deseja Excluir?</h3>
<fieldset>
<legend></legend>
<div class="display-label">UF</div>
<div class="display-field"><b>
@Html.DisplayFor(model => model.SYS_UF_CD)
</b></div>
<div class="display-label">DESCRIÇÃO</div>
<div class="display-field"><b>
@Html.DisplayFor(model => model.SYS_UF_DESC)
</b></div>
</fieldset>
@using (Html.BeginForm()) {
<p>
<input type="submit" value="Excluir" /> |
@Html.ActionLink("Voltar para a Lista", "Index")
</p>
}
@model STC_WEB.Models.
SYS_UF
@{
ViewBag.Title =
"Alterar";
}
-* Edit.cshtml *- (UPDATE)
<
h2>Edit</h2>
<
script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<
script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@
using (Html.BeginForm()) {
@Html.ValidationSummary(
true)
<fieldset>
<legend>Cadastro de UFs</legend>
<div class="editor-label">
@Html.LabelFor(model => model.SYS_UF_CD)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.SYS_UF_CD)
@Html.ValidationMessageFor(model => model.SYS_UF_CD)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.SYS_UF_DESC)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.SYS_UF_DESC)
@Html.ValidationMessageFor(model => model.SYS_UF_DESC)
</div>
<p>
<input type="submit" value="Gravar" />
</p>
</fieldset>
}
<
div>
@Html.ActionLink(
"Voltar para a Lista", "Index")
</
div>
And On.