This question is locked. New answers and comments are not allowed.
Hello
I have developed master-detail grid in MVC project by taking help from demos (Grid->Master Dteail->Ajax Hierarchy).
I have following models:
Master table model:
Detail table model:
I have Index.cshtml under Views/Magazine/ folder as follows:
Finally, controller for the same is as follows:
When I run the page Master grid shows up properly. First Detail row get expanded but with no data rows. There is no error or exception. So, what is the issue? Is there anything missing? Help for the same.
I have developed master-detail grid in MVC project by taking help from demos (Grid->Master Dteail->Ajax Hierarchy).
I have following models:
Master table model:
public class Dad_Magazine_Master { [DisplayName("Type")] [Key] [Required] public string MAGAZINE_TYPE { get; set; } [Required] [DisplayName("Name")] public string MAGAZINE_NAME { get; set; } [Required] [DisplayName("Language")] public string MAGAZINE_LANG { get; set; } [Required] [DisplayName("Duration")] [Range(1, 100)] public int MAGAZINE_DURATION { get; set; } [Required] [DisplayName("Region")] public string MAGAZINE_REGION { get; set; } [DisplayName("Expiry Date")] public int MAGAZINE_EXPIRY_DATE { get; set; } public int? CREATED_BY { get; set; } public DateTime? CREATED_DATE { get; set; } public int? MODIFIED_BY { get; set; } public DateTime? MODIFIED_DATE { get; set; } }public class Dad_Currency_Master { [Key] public int CURRENCY_ID { get; set; } public string MAGAZINE_TYPE { get; set; } public int D_CURRENCY_CODE { get; set; } public int CURRENCY_VALUE { get; set; } public int? CREATED_BY { get; set; } public DateTime? CREATED_DATE { get; set; } public int? MODIFIED_BY { get; set; } public DateTime? MODIFIED_DATE { get; set; } }I have Index.cshtml under Views/Magazine/ folder as follows:
@{ ViewBag.Title = "Magazine Master";}<h2>Magazine Master</h2>@(Html.Telerik().Grid<Dad_Magazine_Master>().Name("Magazines").Columns(col => { col.Bound(r => r.MAGAZINE_TYPE); col.Bound(r => r.MAGAZINE_NAME).Width(100); col.Bound(r => r.MAGAZINE_LANG).Width(20).Title("Language"); col.Bound(r => r.MAGAZINE_DURATION).Width(20).Title("Duration"); col.Bound(r => r.MAGAZINE_REGION).Width(20).Title("Region"); col.Bound(r => r.MAGAZINE_EXPIRY_DATE).Width(20).Title("Exp Date"); col.Bound(r => r.CREATED_BY).Width(50).ReadOnly(true).Title("Created By"); col.Bound(r => r.CREATED_DATE).Format("{0:d}").ReadOnly(true).Title("Created Date"); col.Bound(r => r.MODIFIED_BY).Width(50).ReadOnly(true).Title("Modified By"); col.Bound(r => r.MODIFIED_DATE).Format("{0:d}").ReadOnly(true).Title("Modified Date"); col.Command(cmd => cmd.Edit().ButtonType(GridButtonType.ImageAndText)); col.Command(cmd => cmd.Delete().ButtonType(GridButtonType.ImageAndText)); }) .ToolBar(commands => { commands.Insert().ButtonType(GridButtonType.ImageAndText); }) .DataKeys(keys => keys.Add(repokey => repokey.MAGAZINE_TYPE).RouteKey("magtype")) .DataBinding(dataBinding => dataBinding.Ajax() .Select("Select_Magazine", "Magazine") .Insert("Insert_Magazine", "Magazine") .Update("Update_Magazine", "Magazine") .Delete("Delete_Magazine", "Magazine") ) .Pageable(paging => paging.PageSize(10)) .Sortable() .Filterable() .ClientEvents(events => events.OnRowDataBound("magazine_onRowDataBound")) .DetailView(details => details.ClientTemplate( Html.Telerik().Grid<Dad_Currency_Master>() .Name("CurrencyDetail_<#= MAGAZINE_TYPE #>") .Columns(col => { col.Bound(c => c.D_CURRENCY_CODE).Width(30).Title("Currency"); col.Bound(c => c.CURRENCY_VALUE).Width(30).Title("Currency Value"); col.Bound(c => c.CREATED_BY).Width(50).ReadOnly(true).Title("Created By"); col.Bound(c => c.CREATED_DATE).Format("{0:d}").ReadOnly(true).Title("Created Date"); col.Bound(c => c.MODIFIED_BY).Width(50).ReadOnly(true).Title("Modified By"); col.Bound(c => c.MODIFIED_DATE).Format("{0:d}").ReadOnly(true).Title("Modified Date"); }) .DataBinding(detDataBinding => detDataBinding.Ajax() .Select("Select_Currency", "Magazine", new { magtype = "<#=MAGAZINE_TYPE #>" }) ) .Pageable(paging => paging.PageSize(10)) .Sortable() .Filterable() .ToHtmlString() )) )<script type="text/javascript"> function expandFirstRow(grid, row) { if (grid.$rows().index(row) == 0) { grid.expandRow(row); } } function magazine_onRowDataBound(e) { var grid = $(this).data('tGrid'); expandFirstRow(grid, e.row); }</script>Finally, controller for the same is as follows:
public ActionResult Index() { return View(new GridModel(context.Magazines)); } #region Magazine CRUD [GridAction] public ActionResult Select_Magazine() { return View(new GridModel(context.Magazines)); } [GridAction] public ActionResult Update_Magazine(string magtype) { var repo = context.Magazines.FirstOrDefault(r => r.MAGAZINE_TYPE == magtype); if (repo != null) { repo.MODIFIED_BY = 1; repo.MODIFIED_DATE = DateTime.Now; TryUpdateModel(repo); context.SaveChanges(); return View(new GridModel(context.Magazines)); } return View(new GridModel(context.Magazines)); } [GridAction] public ActionResult Delete_Magazine(string magtype) { var repo = context.Magazines.FirstOrDefault(r => r.MAGAZINE_TYPE == magtype); if (repo != null) { context.Magazines.Remove(repo); context.SaveChanges(); return View(new GridModel(context.Magazines)); } return View(new GridModel(context.Magazines)); } [GridAction] public ActionResult Insert_Magazine() { Dad_Magazine_Master repost = new Dad_Magazine_Master(); repost.CREATED_BY = 1; // Authorization.UserId; repost.CREATED_DATE = DateTime.Now; if (TryUpdateModel(repost)) { context.Magazines.Add(repost); } context.SaveChanges(); return View(new GridModel(context.Magazines)); } #endregion #region Currency CRUD public ActionResult Select_Currency(string magtype) { try { var detail = context.Currency.Where(d => d.MAGAZINE_TYPE.Equals(magtype)); return View("Index", new GridModel(detail)); } catch (Exception ex) { return View("Index",new GridModel()); } } #endregionWhen I run the page Master grid shows up properly. First Detail row get expanded but with no data rows. There is no error or exception. So, what is the issue? Is there anything missing? Help for the same.