Your first grid

Open the project of the previous tutorial. Since, now we need a database, if not already done, please apply all pending migrations to the database.
Open the Package Manager Consolle, go to to your web project folder with: cd src\YourApplicationName, and execute dotnet ef database update

Now we create a Products table. Add the following model to you project:

public enum Currency
{
    [Display(Name = "$")]
    Dollar,
    [Display(Name = "€")]
    Euro,
    [Display(Name = "£")]
    Pound,
    [Display(Name = "¥")]
    Yen
};
public class Product
{
    public int Id { getset; }
    [MaxLength(256)]
    public string Description { getset; }
    [MaxLength(128)]
    public string Name { getset; }
    public decimal Price { getset; }
    public Currency ChosenCurrency { getset; }
    public bool Available { getset; }
 
    
 
}

Then open Data\ApplicationDbContext.cs and add the following property to the Application DBContext:

public DbSet<Product> Products { getset; }

Add a new migration say "business1" with: dotnet ef migrations add business1,
and update the DB again: dotnet ef database update

Now that we have a new DB table, let select "View Data" on the new table in Sql Server Object Explorer, and add some rows.

We need also 2 item ViewModels, one for the list mode, and the other for the detail mode:

public class SimpleProductViewModel 
{
    public int? Id { getset; }
    [MaxLength(128)]
    [ColumnLayout(DetailWidthsAsString = "100 50")]
    [Required]
    [Display(Name = "Name", Order = 400)]
    public string Name { getset; }
    [ColumnLayout(DetailWidthsAsString = "60 30")]
    [Display(Name = "Price", Order = 300)]
    public decimal Price { getset; }
    [Display(Name = "Cur", Order = 280)]
    [ColumnLayout(WidthsAsString = "10", DetailWidthsAsString = "40 20")]
    public Currency ChosenCurrency { getset; }
    [ColumnLayout(DetailWidthsAsString = "20")]
    [Display(Name = "Av", Order = 230)]
    public bool Available { getset; }
 
}   
 
public class SimpleProductViewModelDetailSimpleProductViewModel
{
    [MaxLength(256)]
    [Display(Name = "Description", Order = 100)]
    [ColumnLayout(DetailWidthsAsString = "80")]
    [DisplayFormat(NullDisplayText = "no description available")]
    public string Description { getset; }
}

We need also an overall View ViewModel:

public class SimpleProductlistViewModel
{
    public DataPage<SimpleProductViewModel> Products { getset; }
}

Now we may create our repository class, by inheriting form the CRUDRepository:

public class ProductRepository :
    DefaultCRUDRepository<ApplicationDbContextProduct>
{
    private ApplicationDbContext db;
    public ProductRepository(ApplicationDbContext db)
       : base(db, db.Products) { this.db = db; }
}

In this very simple example our repository doesn't add further methods.

Now it is time of our controller. We will inherit from ServerCrudController<VMD, VMS, D>. This way we have all grid ajax operations already in place with no need to write further code:

public class GridsController :
    ServerCrudController<SimpleProductViewModelDetail,
        SimpleProductViewModel,
        int?>
{
 
    public GridsController(ProductRepository repository,
        IStringLocalizerFactory factory, IHttpContextAccessor accessor)
        : base(factory, accessor)
    {
        Repository = repository;
    }
    [ResponseCache(Duration = 0, NoStore = true)]
    public async Task<IActionResult> ReadonlyServer()
    {
        var model = new SimpleProductlistViewModel
        {
            Products = await Repository.GetPage<SimpleProductViewModel>(
            null,
            q => q.OrderBy(m => m.Name),
            1, 20)
        };
        return View(model);
    }
}

Finally, add a View for the ReadonlyServer action method with the SimpleProductViewModel ViewModel, and with the following code:

<grid asp-for="Products.Data"
      type="Immediate"
      all-properties="true"
      mvc-controller="typeof(LiveExamples.Controllers.GridsController)"
      row-id="readonly-example"
      operations="user => Functionalities.ReadOnly | Functionalities.ShowDetail"
      class="table table-condensed table-bordered" />

and with the following js scripts:

@section Scripts {
    <script src="~/lib/mvcct-controls/mvcct.controls.min.js"></script>
    <script src="~/lib/mvcct-controls/modules/mvcct.controls.ajax.min.js"></script>
    <script src="~/lib/mvcct-controls/modules/mvcct.controls.serverGrid.min.js"></script>
}

Done! Now add a link in the _Layout page as in the previous tutorial and run it!

Our readonly grid shows als data items in detail mode, with a smart layout!

See it live


Fork me on GitHub