Ok posting one more time in this thread...hopefully.
I've got everything working now the way I want but there are two small things happening that I would like to fix.
1. When I add or delete an item from the nested grid, the parent grid collapses instead of remaining open. I thought I had fixed this issue early on but it seems to be happening again now.
2. When I click "Delete" on the nested grid, I get the "Delete this item" pop up twice.
Here is my view code:
@{ Html.Kendo().Grid(Model)
.Name("gridLaboratories")
.Columns(columns =>
{
columns.Command(command => { command.Edit(); }).Width(50);
columns.Bound(l => l.ID);
columns.Bound(l => l.Description);
columns.Command(command => { command.Destroy(); }).Width(50);
})
.DetailTemplate(l =>
{
Html.Kendo().TabStrip()
.Name("tabstrip" + l.ID)
.SelectedIndex(0)
.Items(items =>
{
items.Add().Text("Lab Admins").Content(@<
text
>
@(Html.Kendo().Grid(l.Users_Roles)
.Name("gridLabAdmins" + l.ID)
.Columns(columns =>
{
columns.Bound(a => a.User.BNL_ID).Title("BNL ID");
columns.Bound(a => a.User.Pool.First_Name).Title("First Name");
columns.Bound(a => a.User.Pool.Last_Name).Title("Last Name");
columns.Bound(a => a.User.Account).Title("BNL Account");
columns.Command(command => { command.Destroy(); }).Width(50);
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("SystemAdmin/LabAdmin"))
.Sortable()
.DataSource(dataSource => dataSource
.Server()
.Model(model => model.Id(a => a.ID))
.Read(read => read.Action("Laboratories", "SystemAdmin", new { labID = l.ID }))
.Create(create => create.Action("AddLabAdmin", "SystemAdmin"))
.Destroy(destroy => destroy.Action("DeleteLabAdmin", "SystemAdmin"))
)
)
</
text
>);
})
.Render();
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("SystemAdmin/Laboratory"))
.Pageable()
.Sortable()
.DataSource(dataSource => dataSource
.Server()
.Model(model =>
{
model.Id(l => l.ID);
model.Field(field => field.ID).DefaultValue("");
model.Field(field => field.Description).DefaultValue("");
})
.Create(create => create.Action("AddLaboratory", "SystemAdmin"))
.Read(read => read.Action("Laboratories", "SystemAdmin"))
.Update(update => update.Action("UpdateLaboratory", "SystemAdmin"))
.Destroy(destroy => destroy.Action("DeleteLaboratory", "SystemAdmin"))
)
.RowAction(row =>
{
if (row.DataItem.ID.ToString() == Request.QueryString["labID"])
{
row.DetailRow.Expanded = true;
}
})
.Render();
}
<
script
type
=
"text/javascript"
>
$(document).ready(function () {
var gridMode = getURLParameter("gridLaboratories-mode");
if (gridMode == "edit") {
$("#gridLaboratoriesPopUp").find('input[name="ID"]').attr("disabled", true);
}
});
function getURLParameter(name) {
return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1]);
}
</
script
>
And here is my controller code:
public class SystemAdminController : Controller
{
public ActionResult Index()
{
return View();
}
#region Laboratories
[Authorize(Roles = "System_Admin")]
public ActionResult Laboratories()
{
var context = new PASSEntities();
return View(context.Laboratories.ToList());
}
[Authorize(Roles = "System_Admin")]
[HttpPost]
public ActionResult AddLaboratory(Laboratory laboratory)
{
try
{
using (PASSEntities context = new PASSEntities())
{
context.Laboratories.Add(laboratory);
context.SaveChanges();
}
return RedirectToAction("Laboratories");
}
catch
{
return View();
}
}
[Authorize(Roles = "System_Admin")]
[HttpPost]
public ActionResult DeleteLaboratory(string id, Laboratory laboratory)
{
try
{
using (PASSEntities context = new PASSEntities())
{
context.Entry(laboratory).State = System.Data.EntityState.Deleted;
context.SaveChanges();
}
return RedirectToAction("Laboratories");
}
catch
{
return View();
}
}
[Authorize(Roles = "System_Admin")]
[HttpPost]
public ActionResult UpdateLaboratory(string id, Laboratory laboratory)
{
try
{
using (PASSEntities context = new PASSEntities())
{
context.Entry(laboratory).State = System.Data.EntityState.Modified;
context.SaveChanges();
return RedirectToAction("Laboratories");
}
}
catch
{
return View();
}
}
#endregion
#region Lab Admins
public class EmployeeInfo
{
public string FirstName { get; set; }
public string LastName { get; set; }
public decimal UserID { get; set; }
};
public JsonResult GetEmployeeByBNLID(string bnlID)
{
var context = new PASSEntities();
User employee = context.Users.SingleOrDefault(user => user.BNL_ID == bnlID);
EmployeeInfo employeeInfo = new EmployeeInfo
{
FirstName = employee.Pool.First_Name,
LastName = employee.Pool.Last_Name,
UserID = employee.ID
};
return Json(employeeInfo);
}
[Authorize(Roles = "System_Admin")]
[HttpPost]
public ActionResult AddLabAdmin(Users_Roles user_role)
{
try
{
using (PASSEntities context = new PASSEntities())
{
context.Users_Roles.Add(user_role);
context.SaveChanges();
}
return RedirectToAction("Laboratories");
}
catch
{
return View();
}
}
[Authorize(Roles = "System_Admin")]
[HttpPost]
public ActionResult DeleteLabAdmin(Users_Roles user_role)
{
try
{
using (PASSEntities context = new PASSEntities())
{
context.Entry(user_role).State = System.Data.EntityState.Deleted;
context.SaveChanges();
}
return RedirectToAction("Laboratories");
}
catch
{
return View();
}
}
#endregion
}