Kendo Ui Grid Razor Set Editable Based on Value on Read
-
- Updated date Dec 04, 2020
- 228.8k
- 4
In this article, we will learn how to bind data to Kendo Grid in MVC using multiple ways.
Introduction
Many times, while working with Grid, we need to show the result into a GridView in different situations.
What is Kendo Grid?
The Kendo UI Grid is a powerful widget that allows you to visualize and edit data via its table representation. It provides many options, such as paging, sorting, filtering, grouping, and editing, which determine the way data is presented and manipulated.
The Grid tin be bound to local or remote data by using the Kendo UI DataSource component.
Scenario I Bind data to Kendo Grid by using AJAX Read action method.
Scenario II Change the datasource on alter outcome of any HTML controls.
Scenario I
Commonly, a developer can bind the data to Grid by using AJAX Read method.
This read method is ActionResult method in MVC which will return JSON DataSourceResult & direclty bind the data to Grid.
Annotation
Besides, the developer can pass parameters to this read method. Suppose below is our read method.
- Read(read => read.Activity( "BindGrid" , "Dwelling house" ).Data( "SortByCompanyId" ))
In the in a higher place line, my JavaScript function name is SortByCompanyId which volition render company id. On that basis, we can pass parameter to our activity method in Controller.
- public ActionResult BindGrid([DataSourceRequest]DataSourceRequest request, string companyId)
- {
- }
Our JavaScript role is like the following.
- <script type= "text/javascript" >
- role SortByCompanyId() {
- var cId = 25;
- render {
- companyId: cId
- }
- }
- </script>
Step 1Create a Model, give it a name as Company, and declare some properties into it.
- public class Company
- {
- public int Id { get ; prepare ; }
- public string Name { become ; fix ; }
- public int ? CompanyId { get ; set ; }
- }
Footstep 2Now, create a Controller, requite it proper noun equally HomeController. In this, create a action method as Index() which will return a View. On this View, write some lawmaking to bind data into the Grid View by using the higher up Model.
- @model Grid.Models.Company
- @using System.Web.Optimization
- @using Kendo.Mvc.UI
- @using Kendo.Mvc.Extensions
- @{
- Layout = "~/Views/Shared/_Layout.cshtml" ;
- }
- < div way = "width:60%;pinnacle:100%" >
- @(Html.Kendo().Grid< Grid.Models.Visitor > ()
- .Proper noun("BindGridUsingRead")
- .Columns(columns = >
- {
- columns.Bound(p = > p.Id).Width(15).Title("Sr. No.").Filterable(false);
- columns.Bound(p = > p.Proper noun).Title("Proper name").Width(xxx).Filterable(false);
- columns.Bound(p = > p.CompanyId).Championship("Company Id").Width(fifteen).Filterable(false);
- })
- .Scrollable()
- .Pageable(x = > ten.PageSizes(new Listing < object > { 10, xx, 100, 200, 500, "all" }).Refresh(true))
- .Filterable(ftp = > ftp.Mode(GridFilterMode.Row))
- .Resizable(resize = > resize.Columns(true))
- .HtmlAttributes(new {mode = "superlative: 100%" })
- .Selectable()
- .DataSource(dataSource = > dataSource
- .Ajax()
- .Model(model = > model.Id( p = > p.Id))
- .ServerOperation(fake)
- .Read(read = > read.Action("BindGrid", "Home"))
- )
- )
- </ div >
Stride 3In Controller, write method which will bind the data to Grid past using Ajax Read activity method. Now, write some action methods named every bit BindGrid (to bind the JSON information to Grid). I am going to bind some hard coded values past using action method GetGridData() using Company model which will render IEnumerable Listing.
- public ActionResult Index()
- {
- return View();
- }
- public ActionResult BindGrid([DataSourceRequest]DataSourceRequest asking)
- {
- try
- {
- decimal companyId = 0;
- List<Models.Company> lst =new List<Models.Company>();
- lst = GetGridData(Convert.ToInt32(companyId)).ToList();
- DataSourceResult result = lst.ToDataSourceResult(request, p =>new Models.Company
- {
- Id = p.Id,
- Name = p.Name,
- CompanyId = p.CompanyId,
- });
- return Json(event, JsonRequestBehavior.AllowGet);
- }
- take hold of (Exception ex)
- {
- var errorMsg = ex.Message.ToString();
- return Json(errorMsg, JsonRequestBehavior.AllowGet);
- }
- }
- public IEnumerable<Models.Company> GetGridData( decimal companyId)
- {
- List<Models.Company> objCmp =new List<Models.Company>();
- List<Models.Visitor> listCompany =new List<Models.Company>();
- objCmp.Add(new Models.Visitor() { Id = i, Name = "Rupesh Kahane" , CompanyId = 20 });
- objCmp.Add together(new Models.Visitor() { Id = 2, Proper name = "Vithal Wadje" , CompanyId = xl });
- objCmp.Add(new Models.Company() { Id = iii, Name = "Jeetendra Gund" , CompanyId = 30 });
- objCmp.Add(new Models.Company() { Id = 4, Proper noun = "Ashish Mane" , CompanyId = fifteen });
- objCmp.Add(new Models.Company() { Id = 5, Name = "Rinku Kulkarni" , CompanyId = 18 });
- objCmp.Add(new Models.Company() { Id = 6, Name = "Priyanka Jain" , CompanyId = 22 });
- if (companyId > 0)
- {
- listCompany = objCmp.ToList().Where(a => a.CompanyId <= companyId).ToList();
- return listCompany.AsEnumerable();
- }
- return objCmp.ToList().AsEnumerable();
- }
Step 4Here is the issue after running our solution.
Scenario II
While working with html controls or forms, there are some situations, similar we need to bind the data to Grid on change event of controls or we need to employ some custom filters, on search button click consequence, nosotros are applying these filters that modify the datasource on change issue of any html control.
Notation
In that location is a built-in functionality likewise to filter Grid data from column values. Here, we are using custom filters.
Suppose, the user entered some integer value & we demand the data where company id is less than input value in on change event of control. So we have one textbox & will write on change consequence code into a javascript past using ship.options.read method. Nosotros are passing read method URL & input value to out action method.
Step 1As I explained in scenario I, we are adding only 1 textbox &JS file which contains alter event function to our View. Add together some JavaScript code above this which volition incorporate bind grid method equally -
- <script type= "text/javascript" >
- var gridObject;
- var readURL = new Array();
- readURL[0] ="/Home/BindGrid" ;
- readURL[i] ="/Home/BindGridOnSearch?companyId=" ;
- </script>
Now, our View folio will look similar the following.
- @model Grid.Models.Company
- @using System.Web.Optimization
- @using Kendo.Mvc.UI
- @using Kendo.Mvc.Extensions
- @{
- Layout = "~/Views/Shared/_Layout.cshtml" ;
- }
- < script type = "text/javascript" >
- var gridObject;
- varreadURL = new Array();
- readURL[0] = "/Home/BindGrid";
- readURL[1] = "/Domicile/BindGridOnSearch?companyId =";
- </ script >
- < div >
- < div grade = "box-header with-border" >
- < div course = "col-xs-12" >
- < div class = "col-xs-iv martop" style = "text-align:right;width:22%" >
- @(Html.Kendo().TextBoxFor(model = > model.CompanyId)
- .Name("CompanyId")
- .HtmlAttributes(new {placeholder = "Search Past Company Id" , @ autocomplete = "off" , @ class = "col-sm-12" , manner = "width:100%" , maxlength = 200 })
- )
- </ div >
- </ div >
- </ div >
- </ div >
- < br />
- < div mode = "width:threescore%;superlative:100%" >
- @(Html.Kendo().Grid< Grid.Models.Company > ()
- .Name("BindGridUsingRead")
- .Columns(columns = >
- {
- columns.Leap(p = > p.Id).Width(xv).Title("Sr. No.").Filterable(false);
- columns.Bound(p = > p.Proper name).Title("Name").Width(30).Filterable(false);
- columns.Jump(p = > p.CompanyId).Championship("Company Id").Width(fifteen).Filterable(imitation);
- })
- .Scrollable()
- .Pageable(x = > x.PageSizes(new List < object > { 10, 20, 100, 200, 500, "all" }).Refresh(true))
- .Filterable(ftp = > ftp.Mode(GridFilterMode.Row))
- .Resizable(resize = > resize.Columns(true))
- .HtmlAttributes(new {style = "height: 100%" })
- .Selectable()
- .DataSource(dataSource = > dataSource
- .Ajax()
- .Model(model = > model.Id( p = > p.Id))
- .ServerOperation(false)
- .Read(read = > read.Action("BindGrid", "Domicile"))
- )
- )
- </ div >
- < script src = "~/Scripts/Custom/Home.js" > </ script >
Pace twoAt present, write code on change event of textbox into JavaScript Domicile.js file. In our JS file on windows.load method, nosotros tin get information of Filigree into a our global variable.
- $(window).load( function () {
- gridObject = $("#BindGridUsingRead" ).data( "kendoGrid" );
- });
- $(document).ready(part () {
- $('#CompanyId' ).on( 'change' , function () {
- var cmpId = $( "input[id='CompanyId']" ).val();
- gridObject.dataSource.send.options.read.url = readURL[1] + cmpId;
- gridObject.dataSource.read();
- });
- });
Step iiiWrite some code into Controller against these read methods.
- public ActionResult BindGridOnSearch([DataSourceRequest]DataSourceRequest request, string companyId)
- {
- try
- {
- List<Models.Company> list =new List<Models.Company>();
- list = GetGridData(Convert.ToInt32(companyId)).ToList();
- DataSourceResult result = list.ToDataSourceResult(request, p =>new Models.Company
- {
- Id = p.Id,
- Name = p.Name,
- CompanyId = p.CompanyId,
- });
- return Json(upshot, JsonRequestBehavior.AllowGet);
- }
- catch (Exception ex)
- {
- var errorMsg = ex.Message.ToString();
- return Json(errorMsg, JsonRequestBehavior.AllowGet);
- }
- }
Step 4Here is the result after running our solution (if we enter 25 into input box).
Summary
In this article, yous learned the nuts of how to demark data to Kendo Grid in MVC using multiple methods.
Kendo Ui Grid Razor Set Editable Based on Value on Read
Source: https://www.c-sharpcorner.com/article/multiple-ways-to-bind-data-to-kendo-grid-in-mvc/
0 Response to "Kendo Ui Grid Razor Set Editable Based on Value on Read"
Post a Comment