Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sitefinity - Sample best practices when implementing custom MVC Widgets
using SitefinityWebApp.Mvc.Models;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.Mvc;
using Telerik.Sitefinity.ContentLocations;
using Telerik.Sitefinity.Data;
using Telerik.Sitefinity.DynamicModules;
using Telerik.Sitefinity.DynamicModules.Builder.Model;
using Telerik.Sitefinity.DynamicModules.Model;
using Telerik.Sitefinity.Frontend.Mvc.Helpers;
using Telerik.Sitefinity.Frontend.Mvc.Infrastructure.Controllers;
using Telerik.Sitefinity.GenericContent.Model;
using Telerik.Sitefinity.Mvc;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Utilities.TypeConverters;
using Telerik.Sitefinity.Web;
namespace SitefinityWebApp.Mvc.Controllers
{
// Widget
[ControllerToolboxItem(Title = "Webinar", Name = "Webinar", SectionName = "MVC Widgets")]
public class WebinarController : ContentBaseController, IContentLocatableView
{
#region implement IContentLocatableView
public IEnumerable<IContentLocationInfo> GetLocations()
{
var manager = DynamicModuleManager.GetManager(string.Empty);
//prepare list for all locations
var locations = new List<IContentLocationInfo>();
//add each module location specifics
var webinarLocation = new ContentLocationInfo();
webinarLocation.ContentType = TypeResolutionService.ResolveType(this.Model.TypeName);
webinarLocation.ProviderName = manager.Provider.Name;
// move all locations
locations.Add(webinarLocation);
return locations;
}
public bool? DisableCanonicalUrlMetaTag { get; set; }
#endregion
private WebinarModel model;
[TypeConverter(typeof(ExpandableObjectConverter))]
public virtual WebinarModel Model
{
get
{
if (this.model == null)
this.model = ControllerModelFactory.GetModel<WebinarModel>(this.GetType());
return this.model;
}
}
public ActionResult Index(int? page)
{
var viewModel = this.Model.CreateListViewModel(null, this.ExtractValidPage(page));
var shouldReturnDetails = this.ShouldReturnDetails(this.Model.ContentViewDisplayMode, viewModel);
if (shouldReturnDetails)
{
return this.Details((DynamicContent)viewModel.Items.First().DataItem);
}
SubscribeCacheDependency();
return this.View("Index", this.Model.CreateListViewModel(viewModel));
}
public ActionResult Details(DynamicContent item)
{
this.InitializeMetadataDetailsViewBag(item);
// Further reading at Meta tag setting - https://github.com/Sitefinity/feather/blob/master/Telerik.Sitefinity.Frontend/Mvc/Infrastructure/Controllers/ContentBaseController.cs
// ViewBag.Metadata to obtain more metadata properties
var viewModel = this.Model.CreateDetailsViewModel(item);
return this.View("Detail", viewModel);
}
public ActionResult NotFound(string urlName)
{
return this.View("NotFound");
}
private void SubscribeCacheDependency()
{
var contextItems = SystemManager.CurrentHttpContext.Items;
var isBackendRequest = contextItems.Contains(SystemManager.IsBackendRequestKey) && ((bool)contextItems[SystemManager.IsBackendRequestKey]);
if (isBackendRequest)
{
return;
}
if (!contextItems.Contains(PageCacheDependencyKeys.PageData))
{
contextItems.Add(PageCacheDependencyKeys.PageData, new List<CacheDependencyKey>());
}
var existingKeys = (List<CacheDependencyKey>)contextItems[PageCacheDependencyKeys.PageData];
var keysToAdd = GetCacheDependencyKeys();
existingKeys.AddRange(keysToAdd);
}
private IEnumerable<CacheDependencyKey> GetCacheDependencyKeys()
{
var contentType = TypeResolutionService.ResolveType(this.Model.TypeName);
var cacheDependencyNotifiedObjects = new List<CacheDependencyKey>();
var manager = DynamicModuleManager.GetManager();
string applicationName = manager != null && manager.Provider != null ? manager.Provider.ApplicationName : string.Empty;
cacheDependencyNotifiedObjects.Add(new CacheDependencyKey { Key = string.Concat(ContentLifecycleStatus.Live.ToString(), applicationName, contentType.FullName), Type = typeof(Telerik.Sitefinity.DynamicModules.Model.DynamicContent) });
cacheDependencyNotifiedObjects.Add(new CacheDependencyKey() { Key = contentType.FullName, Type = typeof(DynamicModule) });
return cacheDependencyNotifiedObjects;
}
protected override void HandleUnknownAction(string actionName)
{
this.ActionInvoker.InvokeAction(this.ControllerContext, "NotFound");
}
}
}
@model SitefinityWebApp.Mvc.Models.WebinarModel
<p>
Title: @Model.Title
</p>
<p>
Description: @Model.Description
</p>
<p>
Start: @Model.StartTime
</p>
<p>
End: @Model.EndTime
</p>
@model List<SitefinityWebApp.Mvc.Models.WebinarModel>
@foreach (var item in Model)
{
<p>
Title: @item.Title
</p>
<p>
Description: @item.Description
</p>
<p>
Start: @item.StartTime
</p>
<p>
End: @item.EndTime
</p>
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Telerik.Sitefinity.DynamicModules;
using Telerik.Sitefinity.DynamicModules.Model;
using Telerik.Sitefinity.Frontend.Mvc.Models;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Taxonomies.Model;
using Telerik.Sitefinity.Utilities.TypeConverters;
namespace SitefinityWebApp.Mvc.Models
{
public class WebinarModel : ContentModelBase
{
[Browsable(false)]
public string Title { get; set; }
[Browsable(false)]
public string Description { get; set; }
[Browsable(false)]
public DateTime StartTime { get; set; }
[Browsable(false)]
public DateTime EndTime { get; set; }
public string TypeName
{
get
{
return "Telerik.Sitefinity.DynamicTypes.Model.Webinars.Webinar";
}
}
protected override IQueryable<IDataItem> GetItemsQuery()
{
var manager = DynamicModuleManager.GetManager(string.Empty, "DynamicModuleTransaction-asdf");
var webinarType = TypeResolutionService.ResolveType(TypeName);
return manager.GetDataItems(webinarType);
}
public override ContentListViewModel CreateListViewModel(ITaxon taxonFilter, int page)
{
return base.CreateListViewModel(taxonFilter, page);
}
public List<WebinarModel> CreateListViewModel(ContentListViewModel viewModel)
{
var result = new List<WebinarModel>();
foreach (var dItem in viewModel.Items)
{
var item = (DynamicContent)dItem.DataItem;
result.Add(new WebinarModel
{
Title = item.GetValue<Lstring>("Title"),
Description = item.GetValue<Lstring>("Description"),
StartTime = item.GetValue<DateTime>("StartDate"),
EndTime = item.GetValue<DateTime>("EndDate")
});
}
return result;
}
public WebinarModel CreateDetailsViewModel(DynamicContent item)
{
return new WebinarModel
{
Title = item.GetValue<Lstring>("Title"),
Description = item.GetValue<Lstring>("Description"),
StartTime = item.GetValue<DateTime>("StartDate"),
EndTime = item.GetValue<DateTime>("EndDate")
};
}
}
}
<p>Hi You are not found</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment