How to create Ajax-support Paging Nested Grid in Telerik MVC? - ajax

I try to create a Ajax-support Paging Nested Grid in Telerik MVC Extension. I have rendered the Nested Grid in browse, can expand row Genre to show the related Albums filtered by GenreId, but when i use Ajax for this nested Grid, it don't work in grid paging. I have upload my project and post some code about my View and Controller.
When i don't use ajax, the gird can work properly but it cannot expand correctly when browse is reloaded, how can i maintain the state of row which had expanded?
I'm gratiful for any help.
My Project:
http://share.vnn.vn/dl.php/11718146
My View:
#{
ViewBag.Title = "Home Page";
Layout = #"~\Views\Shared\_Layout.cshtml";
}
#( Html.Telerik().Grid<MvcMusicCodeFirsr.Models.Genre>(#Model)
.Name("personGrid")
.DataKeys(keys => keys.Add(k => k.GenreId))
.DataBinding(d => d.Ajax().Update( "_Update", "Home" )
)
// .Editable(editing => editing.Mode(GridEditMode.PopUp))
.Columns(c =>
{
c.Bound(m => m.Name).Width(150);
c.Bound(m => m.Description).Width(150);
//c.Bound(m => m.Birthdate);
c.Command(commands =>
{
commands.Edit();
}).Width(80);
})
.DetailView(detailView =>
detailView.Template(
#<text>
#(Html.Telerik().Grid<MvcMusicCodeFirsr.Models.Album>(item.Albums)
.Name("Albums_" + item.GenreId)
.DataBinding(d => d.Ajax())
.Columns(columns =>
{
columns.Bound(o => o.Title).Width(101);
columns.Bound(o => o.Price).Width(140);
})
.Pageable()
.Sortable()
.Filterable()
)
</text>
)
.ClientTemplate(
Html.Telerik().Grid<MvcMusicCodeFirsr.Models.Album>()
.Name("Albums_<#=GenreId#>")
.DataBinding(d => d.Ajax())
.Footer(false)
.ClientEvents(events => events.OnDataBinding("detailGrid_dataBinding"))
.ToHtmlString()
))
.Sortable()
.Pageable()
)
)
<script type="text/javascript">
function detailGrid_dataBinding(e) {
var grid = $(this).data("tGrid"),
masterRow = $(this).closest("tr.t-detail-row").prev(),
dataItem = $("#Grid").data("tGrid").dataItem(masterRow);
grid.dataBind(dataItem.Albums);
e.preventDefault();
}
</script>
My Controller:
MusicStoreEntities db = new MusicStoreEntities();
public ActionResult Index()
{
return View(db.Genres.ToList());
}
[GridAction]
public ActionResult _Select()
{
return View(new GridModel ( db.Genres.ToList()));
}
[GridAction]
public ActionResult _Update()
{
return View(new GridModel { Data = db.Genres.ToList() });
}

Here is an example showing ajax pading for the nested grid: http://demos.telerik.com/aspnet-mvc/grid/hierarchyajax

Related

Telerik.UI.for.AspNet.Core in MVC Cannot display data in Grid from a DB context

I think I'm close. Its not throwing any errors but its also not displaying any data... Im just trying to get it to display a list of Company Names and Company IDs from my TblCompanyInfo table.
This is my controller:
public async Task<IActionResult> Index()
{
var apptReminderContext = _context.TblCompanyInfos.Include(t => t.AcctType).Include(t => t.CompanyStatus).Include(t => t.OnHoldReason);
return View(await apptReminderContext.ToListAsync());
//return View();
}
public JsonResult Products_Read([DataSourceRequest] DataSourceRequest request)
{
DataSourceResult result = _context.TblCompanyInfos.ToDataSourceResult(request,
model => new TblCompanyInfo
{
CompanyId = model.CompanyId,
CompanyName = model.CompanyName
});
return Json(result);
}
and my view...
#model IEnumerable<AppointmentRemindersNetCoreMVC.Models.TblCompanyInfo>
#{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
#using AppointmentRemindersNetCoreMVC.Data
#using Kendo.Mvc.UI
#addTagHelper *, Kendo.Mvc
#inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
#Html.AntiForgeryToken()
#(Html.Kendo().Grid<AppointmentRemindersNetCoreMVC.Models.TblCompanyInfo>()
.Name("grid")
.DataSource(dataSource => dataSource.Ajax()
.Read(read => read.Action("Products_Read", "Company"))
.PageSize(20)
//.ServerOperation(false)
//.Model(model => model.Id(c => c.CompanyId))
//.Read("Products_Read", "Company")
//.Read(read => read.Action("Products_Read", "Company"))
.Update("UpdateCustomer", "Home")
.Create("InsertCustomer", "Home")
.Destroy("DeleteCustomer", "Home"))
.Columns(columns =>
{
columns.Bound(product => product.CompanyName);
columns.Bound(product => product.CompanyId);
})
.Pageable()
.Sortable()
)
Also I know that the Products_Read function is being called by the view and I also know that the "result" contains 32 rows of data. However, nothing is displayed in the grid.
Figured it out! Turns out that json camelcases the return string so the model properties did not match what was returned by json. The solution was to add this line to the Startup.cs file.
services.AddControllers()
.AddJsonOptions(options =>
options.JsonSerializerOptions.PropertyNamingPolicy = null);

Using Kendo Grid in MVC with AJAX

I want to show users in a Kendo Grid. Here is my Controller:
public class UserController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Users_Read([DataSourceRequest]DataSourceRequest request)
{
using (var rahatWeb = new RahatWebEntities())
{
IQueryable<User> users = rahatWeb.Users;
DataSourceResult result = users.ToDataSourceResult(request);
return Json(result, JsonRequestBehavior.AllowGet);
}
}
}
Here is my View:
#{
ViewBag.Title = "";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#(Html.Kendo().Grid<RahatWeb.Models.User>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(user => user.Id);
columns.Bound(user => user.FirstName);
columns.Bound(user => user.LastName);
})
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("Users_Read", "User"))
)
.Pageable()
.Sortable()
)
The problem is that no data is shown in Grid. How can I solve the issue?
Have you included kendo.aspnetmvc.min.js in your layout? Also, hit F12 in your browser and check the console for any client-side errors.

filter kendoui grid based on selected another grid value

I have grid in index page and i have another grid in Data page.
On grid on Index page i clicked click view record and then redirect to Data page which also contain the grid. My question is how i can filter grid in Data page based on selected record in grid in Index page.
As you can see, in method GetAllList i tried to filter the grid using rListID which come from the grid on Index page.
Please advise how i can achieve this. Thank you
Index page (View)
#(Html.Kendo().Grid<HApp.Models.SModel>()
.Name("Grid")
.HtmlAttributes(new { #Style = "align:center; font-size:10.5px; length:100%" })
.Columns(columns =>
{
columns.Bound(p => p.RListID).Visible(false);
columns.Bound(p => p.TListID).Visible(false);
columns.Command(commands => commands.Edit()).Width(175);
columns.Command(command => command.Custom("View").Click("OnshowDetails")).Width(150);
})
.Selectable(s => s.Mode(Kendo.Mvc.UI.GridSelectionMode.Single))
.Pageable()
.Scrollable()
.DataSource(dataSource => dataSource
.Ajax()//bind with Ajax instead server bind
.PageSize(10)
.ServerOperation(true)
.Model(model => model.Id(p => p.RListID))
.Read(read => read.Action("GetCData", "CDetails").Type(HttpVerbs.Get))
)
//.Events(events => events
// .Change("change"))
)
<script type="text/javascript">
function OnshowDetails(e) {
var grid = $('#Grid').data('kendoGrid'); //get a reference to the grid data
var record = grid.dataItem(grid.select()); //get a reference to the currently selected row
var rListID = record.RListID;
window.location.href = "#Url.Action("Data ", "CDetails")" + "/?rListID =" + rListID ;
}
</script>
Data Page View
#(Html.Kendo().Grid<HApp.Models.SListsModel>()
.Name("SList")
.HtmlAttributes(new { #Style = "align:center; font-size:10.5px; length:100%" })
.Columns(columns =>
{
columns.Bound(p => p.RListID).Visible(false);
columns.Bound(p => p.CCID);
columns.Command(commands => commands.Edit()).Width(175);
})
.Pageable()
.Selectable(s => s.Mode(Kendo.Mvc.UI.GridSelectionMode.Single))
.Scrollable()
.DataSource(dataSource => dataSource
.Ajax()//bind with Ajax instead server bind
.PageSize(5)
.ServerOperation(true)
.Model(model => model.Id(p => p.CID))
.Read(read => read.Action("GetListData", "CDetails").Type(HttpVerbs.Get))
)
.Events(events => events
.Change("change"))
)
Controller:
public ActionResult Index()
{
return View();
}
public ActionResult Detail()
{
return View();
}
/// <summary>
/// Bind to GetListData
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public ActionResult GetListData([DataSourceRequest] DataSourceRequest request)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
return Json(GetAllList().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}
private static IEnumerable<SListsModel> GetAllList(Guid rListID)
{
var context = new HEntities();
return context.SLists
.Where(filter => filter.RListID== rListID)
.Select(s_list => new SessionListsModel
{
RListID = s_list.RListID,
CCID = s_list.CCID,
});
}
public ActionResult GetCData([DataSourceRequest] DataSourceRequest request)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
return Json(GetAllComList().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}
/// <summary>
/// Get all available session from Session table
/// </summary>
/// <returns></returns>
private static IEnumerable<SModel> GetAllComList()
{
var context = new HEntities();
return context.SM
.Select(com_list => new SModel
{
RListID = com_list.RListID,
PortID = com_list.PortID ,
});
}
To set initial filtering of the Grid I suggest you to use the Filter method of the DataSource configurator object.
.DataSource(dataSource => dataSource
.Ajax()
.Filter(flt=>flt.Add(c=>c.RListID).EndsWith(rListIDValuePassedFromController))

telerik ASP.net MVC Grid Ajax binding issue

I compared the codes with Telerik sample , everything is the same except the model. But I can't see the records in Grid.
// Controller
public ActionResult Index()
{
return View();
}
[GridAction]
public ActionResult _Index()
{
return View(new GridModel<AuctionViewModel>
{
Data = GetData()
}
);
}
// If I replace 'Index' Action codes with '_Index' , the server binding works fine and shows the records but when I try to run AjaxBinding , It doesn't works (never runs _Index codes)
// View
#model List<TestMVC3_Telerik.Models.AuctionViewModel>
#{
Html.Telerik().Grid((List<TestMVC3_Telerik.Models.AuctionViewModel>)ViewData["MyAuctions"])
.Name("Grid")
.Columns(columns =>
{
columns.Bound(o => o.AuctionID).Title("ID").Width(100);
columns.Bound(o => o.AuctionName).Title("Name");
})
.DataBinding(dataBinding => dataBinding.Ajax().Select("_Index", "Grid"))
.Pageable(paging => paging.PageSize(5))
.Sortable()
.Scrollable()
.Groupable()
.Filterable();
}
.DataBinding(dataBinding => dataBinding.Ajax().Select("_Index", "Grid"))
Change "Grid" to what you are calling the page from
.DataBinding(dataBinding => dataBinding.Ajax().Select("_Index", "Home"))
Once I did that it loaded for me.

Telerik.Web.Mvc grid. In the .DetailView not calling the Ajax method

I am using free Telerik.Web.Mvc grid and following this example: http://demos.telerik.com/aspnet-mvc/grid/hierarchyajax
My Issue:
I am populating the grid with search results after user input some data and submit with a search button
In the DetailView() method I reference my 'SearchQuote_QuotesForHierarchyAjax' method, which is in defined in my Controller when DetailView executes data should be fetched, but this controller action does not execute for me.
If i load the grid first time page loads it execute. but not when the grid is loaded in a search button click
The Code in my project:
My SearchQuote.aspx View looks like this
<%= Html.Telerik().Grid(Model.QuoteSummaryList)
.Name("SearchQuoteGrid")
.Columns(columns =>
{
columns.Bound(q => q.QuoteId).Title("Quote #").Width(50);
columns.Bound(q => q.AxiomId).Title("Axiom Id").Width(180);
})
.ClientEvents(events => events.OnRowDataBound("quotes_onRowDataBound"))
.DetailView(details => details.ClientTemplate(
Html.Telerik().Grid(Model.QuoteSubSummaryList)
.Name("Quotes_<#= QuoteId #>")
.Columns(columns =>
{
columns.Bound(o => o.PositionCode).Width(101);
columns.Bound(o => o.Group).Width(140);
})
.DataBinding(dataBinding => dataBinding.Ajax()
.Select("SearchQuote_QuotesForHierarchyAjax", "SearchQuote", new
{quoteid ="<#= QuoteId #>"}))
.Pageable()
.Sortable()
.Filterable()
.ToHtmlString()
))
.DataBinding(dataBinding => dataBinding.Ajax()
.Select("SearchQuote_Select", "SearchQuote"))
.Sortable()
.Pageable(p => p.PageSize(3))
%>
<script type="text/javascript">
function expandFirstRow(grid, row) {
if (grid.$rows().index(row) == 0) {
grid.expandRow(row);
}
}
function quotes_onRowDataBound(e) {
var grid = $(this).data('tGrid');
expandFirstRow(grid, e.row);
}
</script>
And SearchQuoteController has this code.
[AcceptVerbs(HttpVerbs.Post)]
[GridAction]
public ActionResult SearchQuote_QuotesForHierarchyAjax(int quoteid)
{
List<QuoteLineSummaryDM> sublist = new List<QuoteLineSummaryDM>();
QuoteLineSummaryDM a = new QuoteLineSummaryDM();
a.PositionCode = "50";
a.Group = "1";
sublist.Add(a);
QuoteLineSummaryDM b = new QuoteLineSummaryDM();
b.PositionCode = "40";
b.Group = "2";
sublist.Add(b);
var qrows = (from r in sublist
select r).AsQueryable();
return View(new GridModel(qrows));
}
What am I missing? My version is even simpler than the demo. Any ideas?
Thanks.
I found another grid that does what I want to do. It's called jqGrid

Resources