How to use Kendo UI MVC extensions with js requirements?

I have a controller that looks like this:

using System.Collections.Generic; using System.Web.Mvc; using Kendo.Mvc.Extensions; using Kendo.Mvc.UI; namespace KendoMvcApplication.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult GetData([DataSourceRequest] DataSourceRequest req) { var products = CreateProducts(); var result = products.ToDataSourceResult(req); return Json(result); } private static IEnumerable<Product> CreateProducts() { for (int i = 1; i <= 20; i++) { yield return new Product { ProductId = i, ProductName = "Product " + i, ProductPrice = i * 2.5 }; } } } public class Product { public int ProductId { get; set; } public string ProductName { get; set; } public double ProductPrice { get; set; } } } 

And a view that looks like this:

 <!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8" /> <link href="~/Content/kendo.common.min.css" rel="stylesheet" /> <link href="~/Content/kendo.default.min.css" rel="stylesheet" /> <script type="text/javascript" src="~/Scripts/require.js"></script> </head> <body> <div id="grid"></div> <script type="text/javascript"> require.config({ baseUrl : '@Url.Content("~/Scripts")', paths: { 'jquery': 'jquery-2.0.3.min', 'kendo': 'kendo-ui' }, shim: { 'jquery': { exports: 'jQuery' } } }); require(['jquery', 'kendo/kendo.grid.min'], function ($) { $(document).ready(function () { $('#grid').kendoGrid({ dataSource: { schema: { data: 'Data', total: 'Total', aggregates: 'AggregateResults', model: { id: "ProductId", fields: { ProductName: { type: "string" }, ProductPrice: { type: "number" } } } }, transport: { read: { url: "@Url.Action("GetData", "Home")", dataType: "json", method: "post" } }, pageSize: 10, serverPaging: true, serverSorting: true, type: "aspnetmvc-ajax" }, sortable: { mode: "single" }, columns: ["ProductName", "ProductPrice"], scrollable: false, pageable: true }); }); }); </script> </body> </html> 

And my directory structure:

  • Scripts / kendo -ui / * (all kendo files, including mvc one)
  • Scripts /require.js
  • Scripting / jQuery -2.0.3.min.js

which almost works, except that server-side sorting is not applied.

This is because the kendo.aspnet.mvc.min.js file never loads (of course, since JS knows nothing about it), to fix this, I tried this:

 <!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8" /> <link href="~/Content/kendo.common.min.css" rel="stylesheet" /> <link href="~/Content/kendo.default.min.css" rel="stylesheet" /> <script type="text/javascript" src="~/Scripts/require.js"></script> </head> <body> <div id="grid"></div> <script type="text/javascript"> require.config({ baseUrl: '@Url.Content("~/Scripts")', paths: { 'jquery': 'jquery-2.0.3.min', 'kendo': 'kendo-ui', 'kendo-mvc': 'kendo/kendo.aspnetmvc.min' }, shim: { 'jquery': { exports: 'jQuery' } } }); require(['jquery', 'kendo-mvc', 'kendo/kendo.grid.min'], function ($) { $(document).ready(function () { $('#grid').kendoGrid({ dataSource: { schema: { data: 'Data', total: 'Total', aggregates: 'AggregateResults', model: { id: "ProductId", fields: { ProductName: { type: "string" }, ProductPrice: { type: "number" } } } }, transport: { read: { url: "@Url.Action("GetData", "Home")", dataType: "json", method: "post" } }, pageSize: 10, serverPaging: true, serverSorting: true, type: "aspnetmvc-ajax" }, sortable: { mode: "single" }, columns: ["ProductName", "ProductPrice"], scrollable: false, pageable: true }); }); }); </script> </body> </html> 

But this caused this error:

split configuration error

And tried to load js files like this:

try 1 files

Red spots 404 were not found because they are looking for js files in a folder called kendo in the script folder.

So then I thought that I would try to include the whole version, so I tried this:

 <!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8" /> <link href="~/Content/kendo.common.min.css" rel="stylesheet" /> <link href="~/Content/kendo.default.min.css" rel="stylesheet" /> <script type="text/javascript" src="~/Scripts/require.js"></script> </head> <body> <div id="grid"></div> <script type="text/javascript"> require.config({ baseUrl: '@Url.Content("~/Scripts")', paths: { 'jquery': 'jquery-2.0.3.min', 'kendo': 'kendo-ui/kendo.all.min', 'kendo-mvc': 'kendo-ui/kendo.aspnetmvc.min' }, shim: { 'jquery': { exports: 'jQuery' } } }); require(['jquery', 'kendo', 'kendo-mvc'], function ($) { $(document).ready(function () { $('#grid').kendoGrid({ dataSource: { schema: { data: 'Data', total: 'Total', aggregates: 'AggregateResults', model: { id: "ProductId", fields: { ProductName: { type: "string" }, ProductPrice: { type: "number" } } } }, transport: { read: { url: "@Url.Action("GetData", "Home")", dataType: "json", method: "post" } }, pageSize: 10, serverPaging: true, serverSorting: true, type: "aspnetmvc-ajax" }, sortable: { mode: "single" }, columns: ["ProductName", "ProductPrice"], scrollable: false, pageable: true }); }); }); </script> </body> </html> 

But this led to the following errors:

ASP NET MVC Errors

And tried to load js files like this:

case 2 files

In this case, 404 red spots were not found, as they search for files directly in the Scripts folder.

So here is my question:

How to include the specified file in a script of the type of the required JS type?

In addition, I would like to use the kendo.all.min file, not separate ones, because I want to use knockout-kendo in the future and does not seem to work with a separate file, but if the only way to do this is to use a separate file approach, this is normal.

+4
source share
2 answers

It took me a while to get your code to work, but after playing with it a bit, I was able to configure sorting with only a slight change to the source code.

The only thing I changed was on the require line, where I also added the mvc file. Then all the paths became correct, and everything worked out perfectly.

 ['jquery', 'kendo/kendo.grid.min', 'kendo/kendo.aspnetmvc.min'] 

In my code, I used "Kendo UI for ASP.NET MVC Q2 2013" with the jQuery.min.js file that was included in this package. Then the full view code will look like this:

 <script type="text/javascript"> require.config({ baseUrl : '@Url.Content("~/Scripts")', paths: { 'jquery': 'jquery-2.0.3.min', 'kendo': 'kendo-ui' }, shim: { 'jquery': { exports: 'jQuery' } } }); require(['jquery', 'kendo/kendo.grid.min', 'kendo/kendo.aspnetmvc.min'], function ($) { $(document).ready(function () { $('#grid').kendoGrid({ dataSource: { schema: { data: 'Data', total: 'Total', aggregates: 'AggregateResults', model: { id: "ProductId", fields: { ProductName: { type: "string" }, ProductPrice: { type: "number" } } } }, transport: { read: { url: "@Url.Action("GetData", "Home")", dataType: "json", method: "post" } }, pageSize: 10, serverPaging: true, serverSorting: true, type: "aspnetmvc-ajax" }, sortable: { mode: "single" }, columns: ["ProductName", "ProductPrice"], scrollable: false, pageable: true }); }); }); </script> 

I hope this works in your code as well.

+4
source

Try to create a minimal working version. You said that the directory has the following:

  • Scripts / kendo -ui / * (all kendo files, including mvc one)
  • Scripts /require.js
  • Scripting / jQuery -2.0.3.min.js

To download all the dependencies, you can try something like this:

 <html> <body> <script type="text/javascript" src="~/Scripts/require.js"></script> <script type="text/javascript"> require.config({ baseUrl: '@Url.Content("~/Scripts")', paths: { 'jquery': 'jquery-2.0.3.min', 'kendo': 'kendo-ui/kendo.all.min', 'kendo-mvc': 'kendo-ui/kendo.aspnetmvc.min' }, shim: { 'jquery': { exports: 'jQuery' }, 'kendo-mvc' : { deps: ['kendo'] //kendo needs to be loaded before kendo-mvc? } } }); require(['jquery', 'kendo', 'kendo-mvc'], function ($) { }); </script> </body> </html> 

I played by placing it in jsFiddle , but ran into a number of problems (in fact, Kendo requires jQuery 1.9.0, etc.), which you can probably solve by yourself.

It seems that in your latest version kendo.data, kendo.combobox, and many other files are loaded that are not referenced anywhere. Finding out where these requests came from will help solve this mystery.

Update : Here is one opportunity. If kendo-mvc loads dependencies like this:

 ["./kendo.data.min","./kendo.combobox.min","./kendo.multiselect.min","./kendo.โ€Œโ€‹validator.min"] 

It can then fail, because RequireJS looks for dependencies on the name of the module, which was an alias like kendo-mvc. Try not renaming it (see below) and see if this works:

 <script type="text/javascript"> require.config({ baseUrl: '@Url.Content("~/Scripts")', paths: { 'jquery': 'jquery-2.0.3.min', 'kendo-ui/kendo': 'kendo-ui/kendo.all.min', 'kendo-ui/kendo-mvc': 'kendo-ui/kendo.aspnetmvc.min' }, ... require(['jquery', 'kendo-ui/kendo', 'kendo-ui/kendo-mvc'], function ($) { }); 
+2
source

Source: https://habr.com/ru/post/1499551/


All Articles