IMPORTANT: This version of Sitefinity CMS is out of support and the respective product documentation is no longer maintained and can be outdated. Use the version selector to view a supported product version.
The following tutorial demonstrates how to create a simple MVC Books widget. You leverage the attribute routing of MVC 5 in the context of a Sitefinity CMS widget. The Books widget displays a list of books on the frontend and tracks on the client the points retrieved for each book.
BooksWidget
BookWidget.dll
Sitefinity CMS makes it possible to have MVC widgets that are stored in separate assemblies. The following sample creates the Books widget in a separate assembly.
Perform the following:
Telerik.Sitefinity.Core
Telerik.Sitefinity.Mvc
Telerik.Sitefinity.Feather
AssemblyInfo.cs
MVC
MVC\Views
MVC\Views\Books
MVC\Controllers
MVC\Scripts
In the MVC/Models folder, create a new class named Book. The class needs to have:
MVC/Models
Book
Author
Title
Points
Vote
The Book class should look similar to the following:
Next, in the MVC/Models folder, you create a new class named BooksViewModel. The class needs to have the following properties:
BooksViewModel
PageCount
CurrentPage
NextPageUrl
PreviousPageUrl
The BooksViewModel class should look similar to the following:
MVC/Controllers
System.Web.Mvc.Controller
BooksController
.
Index
ControllerToolboxItem
Use the following code sample:
You need to create an Index view, because this is the only view that is used by the BooksController. To do this, you must create a new Razor view named Index and to place it in the MVC/Views/Books folder. To create the Index view, use the following code:
MVC/Views/Books
NOTE: You can create a Razor view in a class library project by selecting HTML Page from the Add New Item dialog, and then renaming the file extension to .cshtml. In the file properties, set the view as Embedded Resource.
.cshtml
In the MVC/Scripts folder, create a JavaScript file named books-widget.js. This script will retrieve and update the books' points calling the JSON actions:
MVC/Scripts
books-widget.js
NOTE: Be sure to mark the script as an Embedded Resource in the file properties.
You can now build the project and test the result by placing the Books widget on a page.
Back To Top
To submit feedback, please update your cookie settings and allow the usage of Functional cookies.
Your feedback about this content is important