• Adding Telerik Extensions for MVC3 using NuGet awesomeness

    by Venkata Koppaka | Feb 04, 2011

    This is fourth in the series posts I am doing to cover Telerik Extensions for ASP.NET MVC3(Updating the MVC3 from today's post).

    1. Getting Started with Telerik Extensions for ASP.NET MVC 
    2. Telerik Extensions for ASP.NET MVC - The Grid
    3. Telerik Extensions for ASP.NET MVC - The Editor 

    In today's post I will talk about using Telerik Controls via Nuget.

    Let us create a new MVC3 application and as with any MVC3 project you get the new ASP.NET View Engine called Razor.

    So let us create a new MVC3 project with Razor view engine.

    We are going to add Telerik Extensions to our MVC3 project via Nuget.

    Open up the Package Manager Console(View->Other Windows->Package Manager Console) and type this line in your PowerShell window

    PM> Install-Package TelerikMvcExtensions.MVC3 

     

    TelerikMVCExtension.MVC3 is the name of the package you want to install.

    Alternatively you can also right click on the webapplication and say Add Library Package Reference.

    By default you will see a list of packages that are installed in your project. To get Telerik Extensions for MVC3 Click Online and type Telerik in the search box

    You can simply hit install button right next to the Package TelerikMVCExtension.MVC3.

    Once you hit install you will see Telerik Scripts (Javascript files) and content(CSS and Image files) automatically added to your application.

    Now all we need to do is add Styles and Register the Telrik Script Registrar in your _Layout.cshtml(Equivalent to masterpage).

    For style links add the following line in the head section of your master page.

        @(Html.Telerik().StyleSheetRegistrar() 
                          .DefaultGroup(group => group 
                              .Add("telerik.common.css") 
                              .Add("telerik.hay.css") 
                              .Combined(true) 
                              .Compress(true)) 
                         ) 

    For registering Telerik Scripts add the following line towards the end of your _Layout.cshtml page.

    @(Html.Telerik().ScriptRegistrar()) 

    That's it. Man doesn't NuGet make adding third party libraries to your application with such ease and awesomeness.?

    You can now use telerik controls in your MVC3 application with the new Razor syntax.

    Hope this helped.

    Cheers,

    Venkata


    Go comment!
  • Telerik Extensions for ASP.NET MVC - The Editor

    by Venkata Koppaka | Jan 19, 2011

    This is third in the series posts I am doing to cover Telerik Extensions for ASP.NET MVC.

    1. Getting Started with Telerik Extensions for ASP.NET MVC 
    2. Telerik Extensions for ASP.NET MVC - The Grid

    In today's post I will talk about using Telerik Extensions for ASP.NET - The Editor.

    I will start with a ASP.NET MVC2 application which is already configured to run Telerik Extensions, if you have not already done that, I encourage you to read my first post on getting started(link above).

    Let us create a new model for holding our data that we want to POST through the editor.

    For this demo let us add a Person Class with two properties.

        public class Person 
        { 
            public int Id { get; set; } 
            public string Name { get; set; } 
     
            public Person(int id, string name) 
            { 
                Id = id; 
                Name = name; 
            } 
        } 

    Now let us add a ActionMethod with just returns a View, let the View be strongly typed to the Person Class we created above.

    public ActionResult CreatePerson() 
            { 
                return View(); 
            } 
    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TelerikEditor.Models.Person>" %> 

    Adding a Telerik Editor is simple and it allows adding properties to the Editor with the help of a Fluent API like other Telerik MVC controls.

    Let us look at basic markup needed to add the Editor.

    <% Html.Telerik().Editor().Name("PersonDescription") 
    .Render();%> 

    If we run the application adding the basic markup for the editor, you should see the editor -

    Let us look deeply into other properties that Editor offers

    <%  
                        Html.Telerik().EditorFor(model=>model.Name) 
                                      .Name("PersonDescription") 
                                      .HtmlAttributes(new { style = "height:200px" }) 
                                      .Value("Please enter your description here") 
                                      .Tools(tool => tool 
                                                    .Clear() 
                                                    .Bold() 
                                                    .Italic() 
                                                    .Underline() 
                                                    .Snippets(snippet=>snippet.Add("Greeting","<h1>Hello World</h1>"))) 
                                      .Render(); 
                    %> 

    Let us look in detail about each of these properties.

    EditorFor for strongly binding the property

    EditorFor is variant of Editor for strongly typing model properties. It takes in the model property in the form of Func<T> you want to type to.

    Name

    The property "Name" is the actual clean HTML id of the table(textarea is wrapped inside aHTML table) that will be used for this editor. After you run the application do a View Source from your browser, you will see a div with the Id you mentioned in this Name property.

    HtmlAttributes

    This property takes in an object of HTML attributes you can apply to the editor.

    Value

    This property is used to set the editor content, the value can be a plain string or a mix of HTML and plain text. If we add some HTML to the value like -

    .Value("<h1>Please enter your description here</h1>") 
    It will automatically be rendered when the editor loads.

    Tools

    There are lots of tools that the editor supports - like Bold, Italic, Underline etc.

    Clear() - clears the current toolbar of the editor and every tool you add after calling clear will appear as in the toolbar.

    Snippets() - Snippets lets you insert pre-configured HTML into the editor.

     

    ClientAPI

    There is a very good client API for the editor that lets you do certain tasks using the editor. To dig more into these I recommend you read Telerk Editor Client API documentation.

    That's it for this post, in the next post I will be talking about how to use the Menu control.

    Hope you found this useful.

    Cheers,

    Venkata

    Go comment!
  • Telerik Extensions for ASP.NET MVC - The Grid

    by Venkata Koppaka | Dec 19, 2010

    This is second in the series posts I am doing to cover Telerik Extensions for ASP.NET MVC.

    1. Getting Started with Telerik Extensions for ASP.NET MVC 

    In today's post I will talk about using Telerik Extensions for ASP.NET - The Grid.

    I will start with a ASP.NET MVC2 application which is already configured to run Telerik Extensions, if you have not already done that, I encourage you to read my first post on getting started(link above).

    Add a Controller and create a method to return IQueryable<YourObject>. For sample code you can use something like this -

        public class AccessLevelController : Controller 
        { 
            private readonly AccessLevelRepository repository = new AccessLevelRepository(); 
     
            public ActionResult Index() 
            { 
                IQueryable<AccessLevel> accessLevelList = repository.GetAll().AsQueryable(); 
                return View(accessLevelList); 
            } 
        } 

    Although it is not mandatory to return a IQueryable<>, Telerik's Grid's Linq Engine will take advantage of IQueryable<> to do sorting, filtering, grouping etc, the engine will fire the queries with the exact filter, sort clauses rather than bringing all the data and trying to filter,sort afterwards. This is a huge performance gain in most of the scenarios.

    Now add a View for the Action Method Index, Strongly type the View to AccessLevel (object I am getting the data for) and leave the view content Empty(we will define our List content using Telerik Grid rather than using the default ASP.NET MVC's List Content)

    Once you add the View change the Model of the view to be IQueryable<AccessLevel> which is what we returned from the Action Method.

    This is how your View should like after changing the Model

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IQueryable<DomainModel.Database.AccessLevel>>" %> 
     
    <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
        Index 
    </asp:Content> 
     
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
     
        <h2>Access Level List</h2> 
     
    </asp:Content> 

    Now, let us add the Telerik Grid.

    Use the below snippet to add Grid -

    <% Html.Telerik().Grid(Model) 
               .Name("AccessLevelList") 
               .Columns(c =>  
               {  
                   c.Bound(o=>o.Access_Level_Id).Title("Access Level Id"); 
                   c.Bound(o => o.Access_Level_Role).Title("Role Name"); 
               }) 
               .PrefixUrlParameters(false) 
               .Sortable() 
               .Pageable() 
               .Filterable() 
               .Groupable() 
               .Render(); 
                %> 

    To test that we did everything correctly let us run the application and see if we see Telerik's Grid.(I will go over each property below).

    If you see a grid which looks like the above image, Great! we are done configuring the Grid.

    Now let us go over what each property in the above HTML means -

    DataBinding using (Grid(Model))

    There are few ways to bind the data to grid, the most easiest and convenient way being to pass theModel to  the grid directly. This model refers to IQueryable<AccessLevel>  which we made the Model of the View. Other ways to bind data are passing data using ViewData collection.

    Name

    The property "Name" is the actual clean HTML id of the div which wraps the table(grid translates to HTML table) that will be used for this list of data. After you run the application do a View Source from your browser, you will see a div with the Id you mentioned in this Name property.

    Columns

    Columns property defines the columns that will be on the grid, it allows the Lambda Syntax to define properties to each column. In the example we are making use of "Title" property for each column to define what the header text of the column will be.

    PrefixUrlParameters

    The PrefixUrlParameters property will tell the Grid whether to prefix the grid name in URL while doing operations like Sorting and Filtering. For example if you run the application with PrefixUrlParameters(true) and sort on Role Name column this is how the URL would change to -

    See how AccessLevelList is added to the URL. This is useful if we have multiple Grid's on the same page, since for the sake of this demo I am using only one grid on the page, I will set PrefixUrlParameters(false) and sort on Role Name column to see how the URL would change to -

    See how AccesLevelList is no longer added in the URL.

    Sortable

    Sortable property makes the grid Sortable, however this property can be turned off on a column by column basis using the Columns property for example if you wish to turn off sorting for AccessLevelId this is how you would do it.

    .Columns(c =>  
               {  
                   c.Bound(o=>o.Access_Level_Id).Title("Access Level Id").Sortable(false); 
                   c.Bound(o => o.Access_Level_Role).Title("Role Name"); 
               }) 

    Pageable

    Pageable property allowing paging for the grid.

    Filterable

    Filterable property allows filtering in the grid, however this property can be turned off on a column by column basis using the Columns property for example if you wish to turn off filtering for AccessLevelId this is how you would do it.

    .Columns(c =>  
               {  
                   c.Bound(o=>o.Access_Level_Id).Title("Access Level Id").Filterable(false); 
                   c.Bound(o => o.Access_Level_Role).Title("Role Name"); 
               }) 

    Groupable

    Groupable property allows grouping of columns in the grid, however this property can be turned off on a column by column basis using the Columns property for example if you wish to turn off Grouping for AccessLevelId this is how you would do it.

    .Columns(c =>  
               {  
                   c.Bound(o=>o.Access_Level_Id).Title("Access Level Id").Groupable(false); 
                   c.Bound(o => o.Access_Level_Role).Title("Role Name"); 
               }) 

    Render

    This property should be called to render any Telerik Extensions control.

    That's it for this post, in the next post I will be talking about how to use the Editor control.

    Hope you found this useful.

    Cheers,

    Venkata

    Go comment!
  • Getting Started with Telerik Extensions for ASP.NET MVC

    by Venkata Koppaka | Dec 12, 2010

    Telerik provides UI extensions to ASP.NET MVC, In this three part series I will  show how to get started with Telerik Extensions for ASP.NET MVC and how to use the Grid and Editor Controls. In this post I will walk through how to get started with these extensions.

    To use Telerik Extensions for ASP.NET MVC go to Telerik Extensions for ASP.NET MVC Page, and download the extensions, you will download a .ZIP, extract them and we are ready to add them to an application.

    With the download you will get Binaries, Content, Scripts and some examples. We will need Binaries, Content and Scripts to start using the Extensions.

    Create a new ASP.NET MVC 2.0 application. Open your Web.config file and look for "Pages" section of your web.config and under "namespaces" add a "Telerik.Web.Mvc.UI" namespace.

    <add namespace ="Telerik.Web.Mvc.UI"/> 

    Then add a reference to "Telerik.Web.Mvc", this DLL can be found under "Binaries" folder of your download.

    Then go to "Scripts" folder of your download, typically you will find a Year, Month, Version folder at the time of this writing it is "2010.3.1110". Inside that folder you will find javascript files for various Telerik UI extensions.

     

    Pick whatever controls you need and copy them to scripts folder of your ASP.NET MVC application. Please copy the folder the extensions where ("2010.3.1110" in this case) these Javascript files are found.

    Once you copy the scripts, the scripts folder in your ASP.NET MVC application should look similar to (of course you could have copied more javascript files)  -

    At the minimum you would need jquery min file and telerik.common.min.js file.

    Then you need to copy the Content(Styles and images) for these controls.These are found under "Content" folder of your download. For this example I am going to use "Hay" theme.Copy the "Hay" folder and telerik.common.min.css and telerik.hay.min.css files to Content folder of your ASP.NET application, once you have done that your Content folder should look similar to -

    Now, open your Site.master file and add link references to these CSS files -

        <link href="/Content/Site.css" rel="stylesheet" type="text/css" /> 
        <link href="/Content/telerik.common.min.css" rel="stylesheet" type="text/css" /> 
        <link href="/Content/telerik.hay.min.css" rel="stylesheet" type="text/css" /> 

    Now towards the bottom of the page, register the Telerik Script Registrar, using this line -

    <% Html.Telerik().ScriptRegistrar().Render(); %> 

    That's it you are now ready use Telerik UI Extensions. In the next post I will show you how to add "Grid" Control to your View page and how to do various operations with it.

    Hope this helped,

    Cheers,

    Venkata

    Go comment!