Using Kendo Grid in MVC with AJAX - 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.

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);

Data showing null in mvc controller when passed through view

I'm new to MVC and using kendo ui grid in my project. I've come up with a issue that I'm getting "null" in controller parameter, although data is passing through view. Please see the below code.
Here is my code part from View
#model IEnumerable<WeBOC.Support.Entities.Vessel>
#{
ViewBag.Title = "Vessels";
}
<h2>Vessels</h2>
#(Html.Kendo().Grid(Model)
.Name("Grid")
.Columns(column =>
{
column.Bound(c => c.VIRNbr).Width(100).ClientTemplate(#Html.ActionLink("#=VIRNbr#", "VesselInspector", new { id = "#=VesselVisitId#" }).ToHtmlString()).Title("VIR No.").Width(150);
column.Bound(c => c.VesselName).Width(150).Title("Vessel Name");
column.Bound(c => c.InboundVoyageNbr).Width(70).Title("IB Vyg");
column.Bound(c => c.OutboundVoyageNbr).Width(70).Title("OB Vyg");
column.Bound(c => c.ETA).Width(100).Title("ETA").Format("{0:dd/MM/yyyy HH:mm}");
column.Bound(c => c.ArrivalDate).Width(100).Title("ATA").Format("{0:dd/MM/yyyy HH:mm}");
})
.Groupable()
.Sortable()
.Pageable()
.Filterable()
.DataSource(datasource => datasource
.Ajax()
.ServerOperation(true)
.PageSize(20)
.Read(read => read
.Action("Vessels_Read", "VesselVisit", new { id = "\\#=State\\#"})
))
)
And this is controller method
public ActionResult Vessels_Read([DataSourceRequest] DataSourceRequest request, string id)
{
return Json(GetVessels(id).ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}
Why I'm getting null in parameter id although passed through view.
public ActionResult Vessels(string id)
{
return View(GetVessels(id));
}
public IEnumerable<Vessel> GetVessels(string phase)
{
IEnumerable<Vessel> vsl = null;
vsl = this._repository.GetVesselByPhase(phase);
return vsl;
}
Any help will be appreciated.
Thanks,
Ovais
If you want to pass additional parameters to your action you should do something like this:
.DataSource( dataSource => dataSource
.Ajax()
.Read( "Vessels_Read", "VesselVisit" ).Data("getState") //getState is a javascript function
)
Then add a script block:
<script>
function getState() {
return {
ID: // your ID, obtained with jQuery, from the View Model, hardcoded, etc...
};
}
</script>
More info here.
EDIT
I have this working right now. I'm working with a View Model but it shouldn't be any different.
View:
.DataSource( dataSource => dataSource
.Ajax()
.Read( "MY_ACTION", "MY_CONTROLLER", new { Id = Model.Id } )
)
Controller:
[HttpPost]
public ActionResult MY_ACTION( [DataSourceRequest] DataSourceRequest request, long Id )
Even if I change new { Id = Model.Id } to new { Id = "QWERTY" } and the action parameter to string, I receive "QUERTY" as the value.

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

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

Ajax binding kendo asp.net Mvc 3 only json displayed

The json result is just being displayed on the screen and not populating the grid.
public ActionResult BulkEdit([DataSourceRequest]DataSourceRequest request)
{        
var NewAssets = db.TurnaroundDumps;
DataSourceResult result = NewAssets.ToDataSourceResult(request)
return Json(result, JsonRequestBehavior.AllowGet);
}
 Then on my view:
#(Html.Kendo().Grid<PcInventory_v1_1.Models.TurnaroundDump>()
.Name("Grid")
.Columns(columns =>
{
columns.Bound(p => p.AssetTag);
columns.Bound(p => p.SerialNumber);
columns.Bound(p => p.DeptId);
columns.Bound(p => p.Location);
})
.DataSource(dataSource => dataSource
.Ajax() // Specify that the data source is of ajax type
.Read(read => read.Action("BulkEdit", "Assets"))
// Specify the action method and controller name
).Pageable()
)
What is going wrong?
I had forgot to return the view.
Here is the answer:
public ActionResult BulkEdit()
{
return View();
}
[HttpPost]
public ActionResult BulkEdit([DataSourceRequest]DataSourceRequest request)
{
var emp = db.TurnaroundDumps;
DataSourceResult result = emp.ToDataSourceResult(request);
return Json(result, JsonRequestBehavior.AllowGet);
}

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.

Resources