CRUD in Grid

Jun 30, 2011 at 8:39 AM

Hi there i just downloaded your simple project and looking this Renderers example, and I am trying to do exactly this.

But there is one issue that's bodering me so I need to ask you first this:

1. I am using a sql databse and entity framework with it, so i don't have data from some custom list like persons in the renderes example,
can I still achieve full grid functionality?

2. Is there any example or tutorial showing how to send data from database in grid, and after cretae/edit/delete actions to show new updated/created/deleted values?

Thank you for any answers and your awesome work!

Developer
Jun 30, 2011 at 10:19 AM

1) It works very well, I’m personally using it for three different projects where two use Entity Framework and one uses LINQ To SQL, I haven’t had any issues.

2) There is no such demo yet – mainly because we’d need to ship a demo database with the source. That’s something we’ve talked about – if someone could suggest a small but not too simple demo db to use and a simple way of including it with the code...

With regards,

mattias

Från: Gbralo [email removed]
Skickat: den 30 juni 2011 09:39
Till: Mattias Fagerlund
Ämne: CRUD in Grid [MVCjQueryControls:263370]

From: Gbralo

Hi there i just downloaded your simple project and looking this Renderers example, and I am trying to do exactly this.

But there is one issue that's bodering me so I need to ask you first this:

1. I am using a sql databse and entity framework with it, so i don't have data from some custom list like persons in the renderes example,
can I still achieve full grid functionality?

2. Is there any example or tutorial showing how to send data from database in grid, and after cretae/edit/delete actions to show new updated/created/deleted values?

Thank you for any answers and your awesome work!

Jun 30, 2011 at 10:41 AM

Thank you for fast response!

Can I ask you of litle help, i need a small example of sending data to grid, but more actions for create and edit, i mean logic when i update a row, how to save it do Db and also refresh page to have updated results.

Developer
Jun 30, 2011 at 10:49 AM
The code below is straight from one of my projects; using the BaseGridController 
(inherits from ContextGridController) allows me to fully implement a grid controller + view in a few minutes.


[Authorize(Users = Administrators.Users)] public class CalculationTypeController : BaseGridController<CalculationType> { public ActionResult Index() { return View(); } protected override GridColumnModelList<CalculationType> GenerateColumns() { GridColumnModelList<CalculationType> columns = new GridColumnModelList<CalculationType>(); columns .Add(p => string.Format("<a href=\"/Verktyg/ROI/Kalkyl/Skapa/{0}\" title='Skapa kalkyl av vad typ'>Skapa</a>", p.CalculationTypeCode)) .SetName("Actions") .SetCaption("�tg�rd") .SetEditable(false) .SetSortable(false) .SetWidth("2"); columns.Add(p => p.CalculationTypeCode).SetAsPrimaryKey().SetWidth("5"); columns.Add(p => p.Name).SetWidth("7"); columns.Add(p => p.Description).SetWidth("15").SetColumnRenderer(new TextAreaColumnRenderer(5, 60)); columns.Add(p => p.Parameters).SetWidth("15").SetColumnRenderer(new TextAreaColumnRenderer(5, 60)); return columns; } public override ActionResult AddItem(CalculationType added, out string newid) { DataContext.CalculationTypes.AddObject(added); DataContext.SaveChanges(); newid = added.CalculationTypeCode; return null; } public override ActionResult DeleteItem(CalculationType toDelete) { CalculationType other = DataContext.CalculationTypes.Single(ct => ct.CalculationTypeCode == toDelete.CalculationTypeCode); DataContext.CalculationTypes.DeleteObject(other); DataContext.SaveChanges(); return null; } public override ActionResult EditItem(CalculationType edited) { CalculationType other = DataContext.CalculationTypes.Single(ct => ct.CalculationTypeCode == edited.CalculationTypeCode); other.Description = edited.Description; other.Name = edited.Name; other.Parameters = edited.Parameters; other.CalculationTypeCode = edited.CalculationTypeCode; DataContext.SaveChanges(); return null; } public override IQueryable<CalculationType> GetItems() { return DataContext .CalculationTypes .OrderBy(ct => ct.Name); } }

    public abstract class BaseGridController<TEntityType> : ContextGridController<CarreteraEntities, TEntityType>
           where TEntityType : class
    {
        protected static GridColumnModelList<TEntityType> Columns { get; private set; }

        protected override CarreteraEntities CreateContext()
        {
            return ContextFactory.CreateCarreteraEntities();
        }

        protected abstract GridColumnModelList<TEntityType> GenerateColumns();

        public override GridColumnModelList<TEntityType> GetColumns()
        {
            if (Columns == null)
            {
                Columns = GenerateColumns();
                Columns.ForEach(col => col.SetAlign("left"));
            }
            return Columns;
        }
    }
}
@using MVC.Controls.Grid;
@using Carretera.Web.Areas.Roi.Controllers;
@using Carretera.Entities;
@{
    ViewBag.Title = "Kalkyltyper";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@MVC.Controls.MVCControlsScriptManager.Instance.RegisterScriptsAndStyles("/Verktyg/Content/MVCControls/", "/Verktyg/Scripts/MVCControls/")
@Html.Grid(
    new GridControl()
        .SetName("grid")
        .SetPageSize(45)
        .SetIsAutoSize(true)
            .UseController<CalculationTypeController>()
        .UpdateDefaultPager(
            pager =>
                pager
                    .ShowAdd(true)
                    .ShowEdit(true)
                    .SetAddEditWidth(650)
                    .ShowDel(true))
        .SetHeight("350"))

 

 

Jun 30, 2011 at 11:15 AM

Thank you, this looks much more familiar to me, view is so simple and nice formated.
You are using DataContext and CalculationType that is something i will have to look more careful.

 

I set context for my base as MVCEntities db = new MVCentities();
and in actions something like: db.SomeModel.Add(item); or db.SomeModel.Find(id);

Well i must say thanks and i hope that i will create something useful!

Developer
Jun 30, 2011 at 11:17 AM

You’re welcome, let us know if you have any further questions

Cheers,

m

Från: Gbralo [email removed]
Skickat: den 30 juni 2011 11:42
Till: Mattias Fagerlund
Ämne: Re: CRUD in Grid [MVCjQueryControls:263370]

From: Gbralo

Thank you for fast response!

Can I ask you of litle help, i need a small example of sending data to grid, but more actions for create and edit, i mean logic when i update a row, how to save it do Db and also refresh page to have updated results.

Jul 1, 2011 at 7:29 AM

I started yesterday to create something but i have one more question (sory for that but i must ask)

I am confused about:

 public class CalculationTypeController : BaseGridController<CalculationType>
I don't see in my solution BaseGridController, i supossed that is one of your personal class.
And CalculationType is one of your model?
I understand pretty much actions for add, edit and delete and in your view:
 .UseController<CalculationTypeController>() -> with that you specify controller to use in that view, and automaticaly by that we don't need to specify action for each controll (add or edit and delete)

So if you could explains litle bit more, i am really thankfull for sharing code but can you explain it please?
Developer
Jul 1, 2011 at 7:32 AM

I'm not sure I understand the question, is it BaseGridController that you can't find? Because that's included in my original source post, after the CalculationTypeController...

Jul 1, 2011 at 7:46 AM

Can you explain me the BaseGridController, I see at the bottom of your sorce this

public abstract class BaseGridController<TEntityType> : ContextGridController<CarreteraEntities, TEntityType>
           where TEntityType : class

what is it for and can i handle my app without that?
Developer
Jul 1, 2011 at 8:39 AM

ContextGridController is included in MVC.Controls, CarreteraEntities are my Entity Framework object context - you'll have one too, it's generated from your edmx file (Model.edmx or whatever it's called)

I use the BaseGridComtroller give me a simple way of implementing new controllers that all use a data context of the type CarreteraEntities.

I guess we need that demo to make it all come together ;)

cheers,
mattias