Hello Communitiy
I use ASP.NET Core 3.1 MVC Web App. I have following codes. I just want to show a the model class objects in the Grid. I alsow want to to a CRUD with the grid. To save the data I use EF Core.
I have no ieda why it doesn't show some data, because from the Request I get data.
Request Response:
{"result":[{"id":"4cf8d89a-a29a-41a7-0c84-08d7913a0404","name":"asdfasdf","categories":null},{"id":"3dfbca0b-a09a-4fe1-be68-8dd67b0b78e8","name":"Income","categories":null}],"count":2}
I feel a bit stupid. I hope you can help. I think it isn't a big problem, but I need the correct hint.
Thanks a lot.
Regards
Gabriel
Model:
public class TransactionType
{
[Key]
public Guid Id { get; set; }
[Required]
public string Name { get; set; }
public IEnumerable<Category> Categories { get; set; }
}
DbContext:
public class BudgetControlDbContext : IdentityDbContext<Member, IdentityRole<Guid>, Guid>
{
public BudgetControlDbContext(DbContextOptions options) : base(options) { }
public DbSet<TransactionType> TransactionTypes { get; set; }
}
Controller:
public class TransactionTypesController : Controller
{
private readonly BudgetControlDbContext _context;
public TransactionTypesController(BudgetControlDbContext context)
{
_context = context;
}
public IActionResult Index() => View();
public IActionResult UrlDatasource([FromBody]DataManagerRequest dataManager)
{
IEnumerable dataSource = _context.TransactionTypes.ToList();
var operation = new DataOperations();
if (dataManager.Search != null && dataManager.Search.Count > 0) dataSource = operation.PerformSearching(dataSource, dataManager.Search);
if (dataManager.Sorted != null && dataManager.Sorted.Count > 0) dataSource = operation.PerformSorting(dataSource, dataManager.Sorted);
if (dataManager.Where != null && dataManager.Where.Count > 0) dataSource = operation.PerformFiltering(dataSource, dataManager.Where, dataManager.Where[0].Operator);
var count = dataSource.Cast<TransactionType>().Count();
if (dataManager.Skip != 0) dataSource = operation.PerformSkip(dataSource, dataManager.Skip);
if (dataManager.Take != 0) dataSource = operation.PerformTake(dataSource, dataManager.Take);
return dataManager.RequiresCounts ? Json(new { result = dataSource, count = count }) : Json(dataSource);
}
public ActionResult Insert(TransactionType value)
{
_context.TransactionTypes.Add(value);
_context.SaveChanges();
return Json(value);
}
public ActionResult Remove(Guid key)
{
_context.TransactionTypes.Remove(_context.TransactionTypes.Find(key));
IEnumerable data = _context.TransactionTypes.ToList();
var count = data.Cast<TransactionType>().Count();
_context.SaveChanges();
return Json(new { result = data, count });
}
public ActionResult Update(TransactionType value)
{
var transaction = value;
var val = _context.TransactionTypes.SingleOrDefault(tt => tt.Id == transaction.Id);
if (val != null) val.Name = transaction.Name;
_context.SaveChanges();
return Json(value);
}
}
My View .cshtml:
@(Html.EJS().Grid<TransactionType>("Grid").DataSource(dataManager =>
{
dataManager.Url("/TransactionTypes/UrlDatasource").Adaptor("UrlAdaptor").InsertUrl("/TransactionTypes/Insert").UpdateUrl("/TransactionTypes/Update").
RemoveUrl("/TransactionTypes/Remove");
}).Width("auto").Columns(col =>
{
col.Field("Id").HeaderText("ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Add();
col.Field("Name").HeaderText("Name").TextAlign(TextAlign.Right).Add();
}).AllowPaging().EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true).Mode(EditMode.Normal); })
.Toolbar(new List<string> { "Add", "Edit", "Delete", "Update", "Cancel" }).Render())