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.
Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.
This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.
This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.
The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.
To submit feedback, please update your cookie settings and allow the usage of Functional cookies.
Your feedback about this content is important