This article continues the series on Episerver All Properties View with describing how to update a property value on the server side and refresh the view.
The scenario and the code shown in the article is building on top of what is described in one of my latest articles.
This time in my scenario, when dropdown is changed, I want to assign that value to the PetTypeName property on the page. This might not sound like much of a challenge, and it is much easier to do on the frontend, but imagine what else can be done by having this action passed through the server side.
First I added the property that I wanted to update on the server side to the page.
1 2 3 4 5 6 7 8 9 10 11 12
// PetPage.cs [ContentType(DisplayName = "PetPage", GUID = "317e0860-8466-4817-8c9e-b33e83a297a4", Description = "")] publicclassPetPage : PageData { // Added code [Display(Name = "Pet Type Name", GroupName = SystemTabNames.Content, Order = 5)] publicvirtualstring PetTypeName { get; set; } // End of added code
Next step was to extend the code that registered dropdown chages. The idea was to refesh the All Properties View, and this is done by getting current pageLink and requesting the view for that value. Here is the official documentation. This soulution is not exactly robust, since it uses setTimeout, but I was not able to figure this part out and it worked good enough.
privatevoidDefaultPropertyValueInitialization_LoadedContent(object sender, ContentEventArgs e) { switch (e.Content) { case PetPage petPage: HandlePetPage(e, petPage); break; } } privatestaticvoidHandlePetPage(ContentEventArgs e, PetPage petPage) { petPage = petPage.CreateWritableClone() as PetPage;
if (petPage != null) { petPage.PetTypeName = Enum.GetName(typeof(PetType), int.Parse(petPage.PetType.SelectedPetType));
e.Content = petPage; } } }
As I final step I added an UIDescriptor to default back to All Properties View, but this shouldn’t be required with the latest version of EpiServer, where it defaults to the last used view.