November Happy Hour will be moved to Thursday December 5th.
November Happy Hour will be moved to Thursday December 5th.
This is how it works:
public override ActionResult Index(ContactBlock currentBlock) { ContactPage contactPage = null; if(!ContentReference.IsNullOrEmpty(currentBlock.ContactPageLink)) { contactPage = _contentLoader.Get<ContactPage>(currentBlock.ContactPageLink); } var linkUrl = GetLinkUrl(currentBlock); var model = new ContactBlockModel { Heading = currentBlock.Heading, Image = currentBlock.Image, ContactPage = contactPage, LinkUrl = GetLinkUrl(currentBlock), LinkText = currentBlock.LinkText, ShowLink = linkUrl != null }; //As we're using a separate view model with different property names than the content object //we connect the view models properties with the content objects so that they can be edited. ViewData.GetEditHints<ContactBlockModel, ContactBlock>() .AddConnection(x => x.Heading, x => x.Heading) .AddConnection(x => x.Image, x => x.Image) .AddConnection(x => (object) x.ContactPage, x => (object) x.ContactPageLink) .AddConnection(x => x.LinkText, x => x.LinkText); return PartialView(model); }
So you can see that the model class (ContactBlock) is transformed to a view model class called "ContactBlockModel". After this we also need to tell Episerver about how we mapped these two by using the ViewData.GetEditHings. If you don't do this, you'll lose the major purpose of PropertyFor which is to get support for editors and on page editing.
3. You need to change the view file to accept your new view model class. In the contact block above that means accepting ContactBlockModel instead like:
@model ContactBlockModel @Html.PropertyFor(x => x.Image)
This file is also normally moved and renamed to /Views/ContactBlock/Index.cshtml now that is has a proper controller and can have multiple actions.
Hope that helps to explain things. So go without a controller for simple views and if you want more advanced stuff, go with controller + view model and don't forget to map properties + change type of model in view + rename/move
Hi!
Im trying to use the @Html.PropertyFor inside a block.. however.. for some reason I keep getting the following error:
"The model item passed into the dictionary is of type 'System.String', but this dictionary requires a model item of type 'XXXXSite.Definitions.Models.Blocks.XXXXBlock'."
This is my entire view:
@model XXXXBlock
@Html.PropertyFor(p => p.NoCasesFound)
if I open up the "Watch" and types Model.NoCasesFound, then it works just fine and I can see my string that I have entered in EPiServer..
Any ideas why PropertyFor fails?
My model inherits from BlockData and the property is set as virtual and public (public virtual string NoCasesFound { get; set; })
EDIT:
I tried to remove my controller for XXXXBlock (XXXXBlockController), and for some reason @Html.Property started to work? Is this the expected behaviour? That we cant use Html.PropertyFor once the model passes through a controller?