Hierarchy Kendo Grid is not working properly in Mvc 4 - asp.net-mvc

I have created a kendo hierarchy grid in mvc 4. On execution of the grid, the hierarchy grid is expand automatically for the first row, but when i expand the another row then the new hierarchy grid comes up empty while the data of the earlier hierarchy grid (which comes up by default) was changed.
1. How can i make it to show data in the hierarchy grid for the
particular row only not in the default opened grid?
2. Also how can i prevent the default expansion of the row.
View:
#(Html.Kendo().Grid<KendoUI.Models.EmployeeViewModel>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(p => p.name).Title("Name");
columns.Bound(p => p.gender).Title("Gender");
columns.Bound(p => p.designation).Title("Designation").Width("300px");
columns.Bound(p => p.department).Title("Department").Width("300px");
})
.ClientDetailTemplateId("template")
.HtmlAttributes(new { style = "height:430px;" })
.Pageable()
.Navigatable()
.Sortable()
.Scrollable()
.DataSource(dataSource => dataSource // Configure the grid data source
.Ajax()
.Model(model =>
{
model.Id(x => x.id);
})
.Read(read => read.Action("Employee_Read", "Home")) // Set the action method which will return the data in JSON format
)
.Events(events => events.DataBound("dataBound"))
)
<script id="template" type="text/kendo-tmpl">
#(Html.Kendo().Grid<KendoUI.Models.EmployeeViewModel>()
.Name("grid_#id#")
.Columns(columns =>
{
columns.Bound(p => p.id).Width(70);
columns.Bound(p => p.name).Width(110);
columns.Bound(p => p.gender).Width(110);
})
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(5)
.Read(read => read.Action("Department_Read", "Home", new { employeeID = "#=id#" }))
)
.Pageable()
.Sortable()
.ToClientTemplate()
)
</script>
<script>
function dataBound() {
this.expandRow(this.tbody.find("tr.k-master-row").first());
}
</script>
Controller:
public ActionResult Employee_Read([DataSourceRequest]DataSourceRequest request)
{
//code for grid
return Json(result, JsonRequestBehavior.AllowGet);
}
public ActionResult Department_Read(Int32 employeeID, [DataSourceRequest]DataSourceRequest request)
{
// Code for hierarchy grid detail
return Json(result, JsonRequestBehavior.AllowGet);
}

You made a mistake in your detail Template
you should use .Name("grid_#=id#") instead of .Name("grid_#id#") the '=' matters.Causing all of your hierarchy-grids to share the same id so only the first grid is refreshed everytime.
your databound() function is causing the expansion of the first by default (and everytime the grid is bound to it datasource.)

Related

Unable to call controller method using kendo file upload

This my kendo grid, where I'm using client detail template to display expanded row. Inside that I need file upload.
This is my view page code.
#(Html.Kendo().Grid<Fundraiser.Web.Models.ProductDescriptors>()
.Name("Grid")
.Columns(columns =>
{
columns.Bound(item => item.ProductName).Title("Product").Width(225);
columns.Bound(item => item.Quantity).Title("Sale Item Quantity").Width(125);
})
.Pageable()
.Sortable()
.Scrollable(scrolling => scrolling.Height("auto"))
.ClientDetailTemplateId("client-template")
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(10)
.Read(read => read.Action("GetProductDetails_Read", "Product").Data("additionalInfo"))
)
)
<script id="client-template" type="text/x-kendo-template">
#(Html.Kendo().Upload()
.Name("fileUpload")
.Async(a => a
.Save("UploadFile", "Product")
.Remove("UploadFile", "Product")
.AutoUpload(true)
)
.ToClientTemplate()
)
</script>
I'm unable to call the controller method using this kendo upload.
[HttpPost]
public void UploadFile(HttpPostedFileBase fileUpload)
{
//Upload file in to server
}

Data binding to the Kendo Grid did not work (Object doesn't support property or method 'kendoGrid')

Please attention to my code.
I passed my object model that there are two items in it but Kendo Grid couldn't show it.
How can I fix this problem?
I've used ASP.NET MVC 5.
EDIT: I investigated it further and I could find a JavaScript error.
Error is:
Object doesn't support property or method 'kendoGrid'
public class ArticleAdminController : Controller
{
private JahanBlogDbContext db = DataContextFactory.GetDataContext();
private readonly IArticleRepository _articleAdminRepository;
public ArticleAdminController(IArticleRepository articleRepository)
{
_articleAdminRepository = articleRepository;
}
public ArticleAdminController()
: this(new ArticleRepository())
{
}
// GET: ArticleAdmin
public async Task<ActionResult> Index([DataSourceRequest] DataSourceRequest request)
{
List<Article> list = await _articleAdminRepository.FindAll().ToListAsync();
return View(list); // list.count = 2
}
public ActionResult Editing_Read([DataSourceRequest] DataSourceRequest request)
{
return Json(_articleAdminRepository.FindAll().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}
}
This is my view code. when program is run, just grid show with no data!
#using Kendo.Mvc.UI
#model IEnumerable<Jahan.Blog.Model.Article>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
#(Html.Kendo().Grid<Jahan.Blog.Model.Article>()
.Name("Grid")
.Columns(columns =>
{
columns.Bound(p => p.UserId);
columns.Bound(p => p.Title).Width(140);
columns.Bound(p => p.Summary).Width(140);
columns.Bound(p => p.Description).Width(100);
columns.Bound(p => p.LikeCounter).Width(20);
columns.Bound(p => p.RateCounter).Width(20);
columns.Bound(p => p.IsActive).Width(20);
columns.Bound(p => p.IsActiveNewComment).Width(20);
columns.Bound(p => p.CreatedDate).Width(20);
columns.Bound(p => p.ModifiedDate).Width(20);
columns.Command(command => command.Destroy()).Width(110);
})
.ToolBar(toolbar =>
{
toolbar.Create();
toolbar.Save();
})
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Pageable()
.Navigatable()
.Sortable()
.Scrollable()
.DataSource(dataSource => dataSource
.Ajax()
.Batch(true)
.PageSize(20)
.ServerOperation(false)
.Events(events => events.Error("error_handler"))
.Model(model => model.Id(p => p.Id))
.Create("Editing_Create", "Grid")
.Read("Editing_Read", "ArticleAdmin")
.Update("Editing_Update", "Grid")
.Destroy("Editing_Destroy", "Grid")
)
)
<script>
function parameterMap(options, operation) {
if (operation !== "read" && options.models) {
return { models: kendo.stringify(options.models) };
}
}
</script>
I found my solution in this page.
If jQuery is included more than once in the page all existing jQuery plugins (including Kendo UI) will be wiped out. Will also occur if the required Kendo JavaScript files are not included.

Create multiple nested Child grids in Kendo UI Grid using ASP.NET MVC

can we implement multiple child nested grids in Kendo UI Grid using ASP.NET MVC i can implement 1 or 2 Child grids but how to implement 5-6 child grids inside one Parent kendo Grid
#(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.EmployeeViewModel>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(e => e.FirstName).Width(110);
columns.Bound(e => e.LastName).Width(110);
columns.Bound(e => e.Country).Width(110);
columns.Bound(e => e.City).Width(110);
columns.Bound(e => e.Title);
})
.Sortable()
.Pageable()
.Scrollable()
.ClientDetailTemplateId("template")
.HtmlAttributes(new { style = "height:430px;" })
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(6)
.Read(read => read.Action("HierarchyBinding_Employees", "Grid"))
)
.Events(events => events.DataBound("dataBound"))
)
<script id="template" type="text/kendo-tmpl">
#(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.OrderViewModel>()
.Name("grid_#=EmployeeID#")
.Columns(columns =>
{
columns.Bound(o => o.OrderID).Width(70);
columns.Bound(o => o.ShipCountry).Width(110);
columns.Bound(o => o.ShipAddress);
columns.Bound(o => o.ShipName).Width(200);
})
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(5)
.Read(read => read.Action("HierarchyBinding_Orders", "Grid", new { employeeID = "#=EmployeeID#" }))
)
.Pageable()
.Sortable()
.ToClientTemplate()
)
</script>
<script>
function dataBound() {
this.expandRow(this.tbody.find("tr.k-master-row").first());
}
</script>
and sample or demo link will be much appreciated. Thanks in advance
Basically you should follow the same approach. The only thing that you should bother is that each Grid should have unique name.
Check the following demo, there are 3 levels of the hierarchy.

Redirect when Edit a Kendo UI grid with ASP .NET MVC

I want to add a redirection to another page when I click on the "Edit" button with a Kendo UI grid with ASP .NET MVC.
Here is the base code:
#(Html.Kendo().Grid<ViewModel>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(x => x.Id);
columns.Bound(x => x.Name);
columns.Bound(x => x.Field1);
columns.Command(commands =>
{
commands.Edit();
commands.Destroy();
})
})
.DataSource(dataSource => dataSource
.Ajax()
.Model(model => model.Id(x => x.Id))
.Read(read => read.Action("Read", "Home"))
.Update(update => update.Action("Edit", "Home"))
.Destroy(destroy => destroy.Action("Destroy", "Home"))
)
)
I tried to use the HTML attributes but it doesn't work:
commands.Edit().HtmlAttributes(new { #class = "edit" });
Then, I tried to add a custom edit (via commands.Custom(...) but unfortunately it is just for .Server() data binding.
I can do it with a client template but I really would like to use the default button proposed by Kendo UI:
columns.Template(#<text></text>)
.ClientTemplate(
"<a href='" + Url.Action("Edit", "Home") + "/#=Id#'>Edit</a>");
Do you have any other idea?
Thanks in advance.
You should be able to use custom commands, even with an Ajax datasource. I just tested this locally with the following code to make sure it will still work.
Code from view:
<script type="text/javascript">
function redirectTest(e) {
e.preventDefault();
var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
alert(dataItem.Name);
}
</script>
#(Html.Kendo().Grid<ViewModel>()
.Name("testing")
.Columns(columns =>
{
columns.Bound(x => x.Id);
columns.Bound(x => x.Name);
columns.Command(command => command.Custom("Edit").Click("redirectTest"));
})
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("ReadAction", "ControllerName"))
)
)
Source: Custom command demo
If you want to redirect to another page with accepts a parameter then use this method.
#(Html.Kendo().Grid<AGridViewModel>()
.Name("Grid")
.Columns(columns => {
columns.Bound(p => p.Id)
columns.Bound(p => p.Name);
columns.Command(command => command.Custom("View Details").Click("showDetails"));
})
<script type="text/javascript">
function showDetails(e) {
e.preventDefault();
var d = this.dataItem($(e.currentTarget).closest("tr"));
//alert("Selected item ID is:" + d.Id);
window.location.href = "#Url.Action("action", "controller")?id=" + d.Id;
}
</script>
You could add a custom ClientTemplate on the Name column, instead, and remove your columns.Command button altogether, where they simply click the name to edit it, and you pass in your object's ID to the new page like this:
columns.Bound(x => x.Name).ClientTemplate("<a href=/SomeViewFolder/Index?id=${id} target=_blank>${Name}</a>");

how to get key row selected in kendo ui grid

i write this code for create Grid with kendo Ui in asp.net mvc
#(Html.Kendo().Grid(Model)
.Name("Grid")
.Columns(columns =>
{
columns.Bound(p => p.Id).Groupable(false).Visible(false);
columns.Bound(p => p.BrandName);
columns.Bound(p => p.BrandAbbr);
columns.Bound(p => p.SrcImage);
columns.Command(command => command.Custom("ViewDetails").Click("showDetails"));
})
.ToolBar(toolbar =>
{
toolbar.Custom().Action("Create","Users").Text("add");
}
)
.Groupable()
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.HtmlAttributes(new {style = "height:500px;"})
.Selectable(selectable => selectable
.Mode(GridSelectionMode.Multiple)
.Type(GridSelectionType.Row))
.DataSource(dataSource => dataSource
.Server()
.Model(model => model.Id(item => item.Id))
))
i want when user click on ViewDetails alert BrandId value Column, please help me.thanks all
You just need to add javascript function.
<script type="text/javascript">
function showDetails(e) {
e.preventDefault();
var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
alert(dataItem.Id); //considering Id = BrandId
}
</script>
Here is the demo of Kendo Grid Custom Command
also I used this successfully :
<script type="text/javascript">
function showDetails(e)
{
e.preventDefaults();
var grid = $("#Grid").data("kendoGrid");
var selectedItem = grid.dataItem(grid.select());
//you can get the value of any column after that
alert("Brand Id is : " + selectedItem.Id);
alert("Brand Name is: " + selectedItem.BrandName);
}
</script>

Resources