Is it possible to define a custom rendering for a PageReference property?

Vote:
 

I am using MVC for a project and I have a page with a property called ReferenceLink and it's of type PageReference. When I render that property in my view, is it possible to use PropertyFor and render the ReferenceLink differently than the default? I thought I could with a Tag on the PropertyFor method, but I would want it to call a controller to populate a ViewModel with the specific properties I need. All pages inherit from a base that has Thumbnail, Description, and Title properties defined on it, so I was going to use the PageReference to get the page and those properties and, render a specific partial that takes a model type of that ViewModel.

Then again, maybe I am going about this all the wrong way. Does anyone have some pointers or ideas on how to approach this? Is this a case where I would want to create a block?

Thanks.

#147120
Apr 06, 2016 15:19
Vote:
 

Hi Daved,

Can you show us how your controller, viewmodel, and view look like?

Let's say your project is based on Alloy :)

You can define a new property on start page:

[ContentType(GUID = "...")]
public class StartPage : PageData
{
    public virtual PageReference MyPage { get; set; }	
	...
}

view model:

public class StartPageViewModel : PageViewModel<StartPage>
{
    public SomePageData MyPage { get; set; }

    public StartPageViewModel(StartPage currentPage) : base(currentPage)
    {
    }
}

controller:

public class StartPageController : PageController<StartPage>
{
    private readonly IContentLoader _contentLoader;

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

    [ContentOutputCache]
    public ActionResult Index(StartPage currentPage)
    {
        var viewModel = new StartPageViewModel(currentPage);
        if (!ContentReference.IsNullOrEmpty(currentPage.MyPage))
        {
            viewModel.MyPage = _contentLoader.Get<SomePageData>(currentPage.MyPage);
        }

        return View(viewModel);
    }
}

Index.cshtml:

@using ...
@model StartPageViewModel
@Html.FullRefreshPropertiesMetaData(new[] { "MyPage" })

@{ Html.RenderPartial("_MyPage", Model); }

_MyPage.cshtml:

@using ...
@model StartPageViewModel

@if (!PageEditing.PageIsInEditMode && Model.MyPage == null)
{
    return;
}

<div @Html.EditAttributes(x => x.CurrentPage.MyPage)>
    ...
</div>
#147143
Apr 06, 2016 23:20
Vote:
 

I think what I was missing with my initial approach was the type of object being passed to the controller. I ended up taking a different approach with this, however, as more properties and data was needed for this paticular item, causing me to create a block.

Thanks for helping, though. I appreciate the sample above.

#147362
Apr 12, 2016 22:19
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.