Master Detail CRUD Demo using Grid and PopupForm

This is a demo for master detail CRUD using the Grid.
For master-detail grid see Master Detail Grid, or Hierarchy (Nested Grids)
MasterDetailCrudDemo/Index.cshtml
@Html.InitCrudPopupsForGrid("RestaurantGrid", "MasterDetailCrudDemo", 470)

<div class="bar">
<button type="button" class="awe-btn mbtn" onclick="awe.open('createRestaurantGrid')">Create</button>
</div>

@(Html.Awe().Grid("RestaurantGrid")
.Height(350)
.Url(Url.Action("RestaurantGridGetItems"))
.Groupable(false)
.Columns(
new Column { ClientFormat = ".Id", Header = "Id", Width = 70 },
new Column { Bind = "Name" },
new Column { ClientFormat = GridUtils.EditFormatForGrid("RestaurantGrid"), Width = 50 },
new Column { ClientFormat = GridUtils.DeleteFormatForGrid("RestaurantGrid"), Width = 50 }))

MasterDetailCrudDemo/Create.cshtml
@model AwesomeMvcDemo.ViewModels.Input.RestaurantInput

@using (Html.BeginForm())
{
@Html.EditorFor(o => o.Id)
@Html.EditorFor(o => o.Name)
}

@Html.InitCrudPopupsForGrid("AddressesGrid", "AddressesGridCrud", 230)

<div class="bar">
<button type="button" class="awe-btn" onclick="awe.open('createAddressesGrid', { params: {restaurantId: '@(Model.Id)' }})">Add address</button>
</div>

@(Html.Awe().Grid("AddressesGrid")
.Url(Url.Action("GridGetItems", "AddressesGridCrud"))
.Parameter("restaurantId", Model.Id)
.Height(230)
.Groupable(false)
.Columns(
new Column { Bind = "Line1,Line2", ClientFormat = ".Line1 .Line2", Header = "Address" },
new Column { ClientFormat = GridUtils.EditFormatForGrid("AddressesGrid"), Width = 52 },
new Column { ClientFormat = GridUtils.DeleteFormatForGrid("AddressesGrid"), Width = 52 }))
AddressesGridCrud/Create.cshtml
@model AwesomeMvcDemo.ViewModels.Input.RestaurantAddressInput

@using (Html.BeginForm())
{
@Html.EditorFor(o => o.RestaurantId)
@Html.EditorFor(o => o.Line1)
@Html.EditorFor(o => o.Line2)
}
Demos/Grid/MasterDetailCrudDemoController.cs
using System;
using System.Linq;
using System.Web.Mvc;

using AwesomeMvcDemo.Models;
using AwesomeMvcDemo.ViewModels.Input;

using Omu.AwesomeMvc;

namespace AwesomeMvcDemo.Controllers.Demos.Grid
{
public class MasterDetailCrudDemoController : Controller
{
public ActionResult Index()
{
return View();
}

public ActionResult RestaurantGridGetItems(GridParams g)
{
var model = new GridModelBuilder<Restaurant>(Db.Restaurants.Where(o => o.IsCreated).AsQueryable(), g)
{
Key = "Id",
GetItem = () => Db.Get<Restaurant>(Convert.ToInt32(g.Key))
}.Build();
return Json(model);
}

public ActionResult Create()
{
// needed so we could add addresses even before the restaurant is created/saved
var rest = Db.Insert(new Restaurant());

return PartialView(new RestaurantInput { Id = rest.Id });
}

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

var restaurant = Db.Get<Restaurant>(input.Id);
restaurant.Name = input.Name;
restaurant.IsCreated = true;

return Json(restaurant); // use MapToGridModel like in Grid Crud Demo when grid uses Map
}

public ActionResult Edit(int id)
{
var rest = Db.Get<Restaurant>(id);
return PartialView("Create", new RestaurantInput { Id = id, Name = rest.Name });
}

[HttpPost]
public ActionResult Edit(RestaurantInput input)
{
if (!ModelState.IsValid)
{
return PartialView("Create", input);
}

var rest = Db.Get<Restaurant>(Convert.ToInt32(input.Id));

rest.Name = input.Name;

return Json(new { rest.Id });
}

public ActionResult Delete(int id, string gridId)
{
var restaurant = Db.Get<Restaurant>(id);

return PartialView(new DeleteConfirmInput
{
Id = id,
Message = string.Format("Are you sure you want to delete restaurant <b>{0}</b> ?", restaurant.Name)
});
}

[HttpPost]
public ActionResult Delete(DeleteConfirmInput input)
{
Db.Delete<Restaurant>(input.Id);
return Json(new { input.Id });
}
}
}
Demos/Grid/AddressesGridCrudController.cs
using System;
using System.Linq;
using System.Web.Mvc;

using AwesomeMvcDemo.Models;
using AwesomeMvcDemo.ViewModels.Input;

using Omu.AwesomeMvc;

namespace AwesomeMvcDemo.Controllers.Demos.Grid
{
public class AddressesGridCrudController : Controller
{
public ActionResult GridGetItems(GridParams g, int restaurantId)
{
var items = Db.RestaurantAddresses.Where(o => o.RestaurantId == restaurantId).AsQueryable();
var model = new GridModelBuilder<RestaurantAddress>(items, g)
{
Key = "Id",
GetItem = () => Db.Get<RestaurantAddress>(Convert.ToInt32(g.Key))
}.Build();
return Json(model);
}

public ActionResult Create(int restaurantId)
{
return PartialView(new RestaurantAddressInput { RestaurantId = restaurantId });
}

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

var address = Db.Insert(new RestaurantAddress { Line1 = input.Line1, Line2 = input.Line2, RestaurantId = input.RestaurantId });

return Json(address); // use MapToGridModel like in Grid Crud Demo when grid uses Map
}

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

return PartialView(
"Create",
new RestaurantAddressInput
{
Line1 = address.Line1,
Line2 = address.Line2,
RestaurantId = address.RestaurantId
});
}

[HttpPost]
public ActionResult Edit(RestaurantAddressInput input)
{
if (!ModelState.IsValid)
{
return PartialView("Create", input);
}

var address = Db.Get<RestaurantAddress>(input.Id);
address.Line1 = input.Line1;
address.Line2 = input.Line2;
return Json(new { input.Id });
}

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

return PartialView(new DeleteConfirmInput
{
Id = id,
Message = string.Format("Are you sure you want to delete restaurant address <b>{0} {1}</b> ?", address.Line1, address.Line2)
});
}

[HttpPost]
public ActionResult Delete(DeleteConfirmInput input)
{
Db.Delete<RestaurantAddress>(input.Id);
return Json(new { input.Id });
}
}
}

Master Detail CRUD using in nest editing

MasterDetailCrudDemo/Index.cshtml
@Html.InitCrudForGridNest("RestaurantGrid2", "MasterDetailCrudDemo")

<div class="bar">
<button type="button" onclick="utils.nestCreate('RestaurantGrid2', 'createRestaurantGrid2')" class="awe-btn mbtn">Create</button>
</div>

@(Html.Awe().Grid("RestaurantGrid2")
.Url(Url.Action("RestaurantGridGetItems"))
.PageSize(15)
.Nests(
new Nest { ButtonClass = "editnst", GetFunc = "utils.loadNestPopup('editRestaurantGrid2')" },
new Nest { ButtonClass = "delnst", GetFunc = "utils.loadNestPopup('deleteRestaurantGrid2')" })
.Columns(
new Column { ClientFormat = ".Id", Header = "Id", Width = 70 },
new Column { Bind = "Name" },
new Column { ClientFormat = GridUtils.EditGridNestFormat(), Width = 50 },
new Column { ClientFormat = GridUtils.DeleteGridNestFormat(), Width = 50 }))