How can I use Kendo UI with Razor?

2019-07-21 04:36发布

问题:

I have downloaded and used Kendo UI with Kendo UI grids, but my source code was very complicated, for several reasons:

  1. I've hacked the sort to enable case insensitive sorting.

  2. I've observed UI bugs when displaying filter menu, page size selector, filter function menu and DatePicker in the filter menu; I've solved these problems with ugly hacks.

  3. On client-side I've refreshed the grid when needed using parameterized posts.

  4. On server-side I have created a function which handled the sort, filter, page size and paging state of the grid dynamically.

However, my client told me that we need a "simple" solution, a grid page should be completed in an hour. I think this is unrealistic with my current approach unless I implement a generalized class to handle the grids. This would be possible using Linq, to handle the tables, fields, filters, sorts, paging and page size. I know this for sure, as my code is not so far from being a general-purpose grid supporter on server-side and a Kendo UI grid factory on client-side. However, my client clearly stated that we don't need to implement this class and prototype, because we should be able to configure Kendo UI simply. He told me (the previously unspecified detail) that we are allowed to return all the rows from a table and filter/sort that on client-side, so points 3 and 4 would become unneeded.

I have been looking at the example here. I would like to have something similar as the example in cshtml. However, in my downloaded Telerik Kendo UI I don't have any server-side content, so the IDE is showing that Html.Kendo().Grid(Model) is incorrect. The error is as follows:

Error 10 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'Kendo' and no extension method 'Kendo' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

I am using ASP.NET MVC3 with Linq.

How can I use Telerik Kendo UI with ASP.NET Razor in the style described in the cshtml file in the link? Is this gratis? If no, how much is the cost? Should I install KendoUI.Mvc.VSPackage.vsix?

回答1:

In order to use additional helpers, you need to register them with Razor view engine. This is done in web.config file, or using @using statement on top of .cshtml file.

Here's an example from my web.config

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />
        <add namespace="Kendo.Mvc.UI"/>
      </namespaces>
    </pages>
  </system.web.webPages.razor>

As far as licensing goes, this is not a freeware software. You can see licensing terms and prices on their website. As per their license, each developer which uses Telerik controls needs to have a license. License is not company wide, or project wide.

You don't need to install vsix, but it could make your life easier by automatically adding references to required assemblies, and adding required entries to web.config files.



回答2:

Also you might want to add a reference to Kendo UI if you're using MVC 5. You do this by going to References in your project, right click and add reference. If it isn't listed click on Browse then browse to the lib folder in your project and click on Kendo.Mvc.dll then add. Hope this helps someone.