Awesome ASP.net Core and MVC Controls

Grid CRUD

Grid crud, built using the Grid and PopupForm helpers.
There are three PopupForms for create, edit and delete, they are initialized via InitCrudPopupsForGrid custom helper, each PopupForm has Success js function assigned.
Create post action returns the grid model for the new Item so the js func will render and append it.
Edit post action returns the item's Id and the js func is using grid.api.update to update the row (api.update will call GridModelBuilder.GetItem).
Delete PopupForm has OnLoad js func used to animate the row that is about to be deleted, and the post action will delete the item and return the item's Id, the js func will use grid.api.select to select and remove the row, if there's no rows left grid.api.load will be called.
Shared/Demos/GridCrud.cshtml
@Html.InitCrudPopupsForGrid("DinnersGrid", "DinnersGridCrud")

<div class="bar">
<div style="float: right;">
@Html.Awe().TextBox("txtSearch").Placeholder("search...").CssClass("searchtxt")
</div>

@(Html.Awe().Button()
.Text("Create")
.OnClick(Html.Awe().OpenPopup("createDinnersGrid")))
</div>

@(Html.Awe().Grid("DinnersGrid")
.Mod(o => o.ColumnsSelector().ColumnsAutohide().PageInfo())
.Url(Url.Action("GridGetItems", "DinnersGridCrud"))
.Parent("txtSearch", "search")
.Attr("data-syncg", "dinner")
.Height(350)
.Columns(
new Column { Bind = "Id", Width = 55 },
new Column { Bind = "Name" }.Mod(o => o.Nohide()),
new Column { Bind = "Date" },
new Column { Bind = "Chef.FirstName,Chef.LastName", ClientFormat = ".(ChefName)", Header = "Chef" },
new Column { ClientFormat = ".(Meals)", Header = "Meals" },
new Column { ClientFormat = Html.EditFormatForGrid("DinnersGrid"), Width = 55 }.Mod(o => o.Nohide()),
new Column { ClientFormat = Html.DeleteFormatForGrid("DinnersGrid"), Width = 55 }.Mod(o => o.Nohide())))
Demos/Grid/DinnersGridCrudController.cs
/// </summary>
/// Edit and Delete post actions must return an object with property "Id" - in utils.js itemEdited and itemDeleted funcs expect it this way;
/// parameters for Edit, Delete controller Actions need to remain called "id", that's how they are set in GridUtils.cs (params:{{ id: );
/// or in MapToGridModel additionally to o.MyId add another property Id = o.MyId
/// and for the action columns you can either specify the MyId property (e.g. GridUtils.EditFormatForGrid("DinnersGrid", "MyId"));
/// change the Bind = "MyId" (if you're showing the id column),
/// Note: If your Key Property is not "Id" (but "MyId"), you need to change the Key = "MyId" in GridGetItems and it in the view
/// <summary>
public class DinnersGridCrudController : Controller
{
private static object MapToGridModel(Dinner o)
{
return
new
{
o.Id,
o.Name,
Date = o.Date.ToShortDateString(),
ChefName = o.Chef.FirstName + " " + o.Chef.LastName,
Meals = string.Join(", ", o.Meals.Select(m => m.Name))
};
}

public ActionResult GridGetItems(GridParams g, string search)
{
search = (search ?? "").ToLower();
var items = Db.Dinners.Where(o => o.Name.ToLower().Contains(search)).AsQueryable();

return Json(new GridModelBuilder<Dinner>(items, g)
{
Key = "Id", // needed for api select, update, tree, nesting, EF
GetItem = () => Db.Get<Dinner>(Convert.ToInt32(g.Key)), // called by the grid.api.update ( edit popupform success js func )
Map = MapToGridModel
}.Build());
}

public ActionResult Create()
{
// make sure to use "return PartialView" for PopupForm/Popup views
// this will ignore _viewstart.cshtml so that you don't use the _Layout.cshtml and reload all the scripts
return PartialView();
}

[HttpPost]
public ActionResult Create(DinnerInput input)
{
if (!ModelState.IsValid) return PartialView(input);

var dinner = Db.Insert(new Dinner
{
Name = input.Name,
Date = input.Date.Value,
Chef = Db.Get<Chef>(input.Chef),
Meals = input.Meals.Select(mid => Db.Get<Meal>(mid)),
BonusMeal = Db.Get<Meal>(input.BonusMealId)
});

return Json(MapToGridModel(dinner)); // returning grid model, used in grid.api.renderRow
}

public ActionResult Edit(int id)
{
var dinner = Db.Get<Dinner>(id);

var input = new DinnerInput
{
Id = dinner.Id,
Name = dinner.Name,
Chef = dinner.Chef.Id,
Date = dinner.Date,
Meals = dinner.Meals.Select(o => o.Id),
BonusMealId = dinner.BonusMeal.Id
};

return PartialView("Create", input);
}

[HttpPost]
public ActionResult Edit(DinnerInput input)
{
if (!ModelState.IsValid) return PartialView("Create", input);
var dinner = Db.Get<Dinner>(input.Id);

dinner.Name = input.Name;
dinner.Date = input.Date.Value;
dinner.Chef = Db.Get<Chef>(input.Chef);
dinner.Meals = input.Meals.Select(mid => Db.Get<Meal>(mid));
dinner.BonusMeal = Db.Get<Meal>(input.BonusMealId);
Db.Update(dinner);

// returning the key to call grid.api.update
return Json(new { Id = dinner.Id });
}

public ActionResult Delete(int id)
{
var dinner = Db.Get<Dinner>(id);

return PartialView(new DeleteConfirmInput
{
Id = id,
Type = "dinner",
Name = dinner.Name
});
}

[HttpPost]
public ActionResult Delete(DeleteConfirmInput input)
{
Db.Delete<Dinner>(input.Id);
return Json(new { Id = input.Id });
}
}
DinnersGridCrud/Create.cshtml
@model AwesomeMvcDemo.ViewModels.Input.DinnerInput
@using (Html.BeginForm())
{
using (Html.Awe().BeginContext())
{
@Html.EditorFor(o => o.Name)
@Html.EditorFor(o => o.Date)
@Html.EditorFor(o => o.Chef)
@Html.EditorFor(o => o.Meals)
@Html.EditorFor(o => o.BonusMealId)
}
}



Comments