Fill CurrentPage after PostBack in MVC

Posted on

public class PageContextActionFilter : IResultFilter
{
    private readonly IContentLoader _contentLoader;

    public PageContextActionFilter(IContentLoader contentLoader)
    {
        _contentLoader = contentLoader;
    }

    public void OnResultExecuting(ResultExecutingContext filterContext)
    {
        var model = filterContext.Controller.ViewData.Model as IPageViewModel<StandardPage>;
        if (model == null)
            return;

        // code like in Alloy Template

        // When ViewModel is posted back to server by POST, CurrentPage is null, so its set here
        if (model.CurrentPage == null)
        {
            var pageReferene = filterContext.RequestContext.GetContentLink();
            var concreteModel = model as PageViewModel<StandardPage>;
            if (concreteModel != null)
            {
                var currentPage = _contentLoader.Get<StandardPage>(pageReferene);
                concreteModel.CurrentPage = currentPage;
            }
        }
    }

    public void OnResultExecuted(ResultExecutedContext filterContext) { }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s