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);
}
Related
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);
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.
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.
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))
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.