Sitefinity 3.6 new backend architecture How to navigate among Views: How do I pass parameters between Views?

Sitefinity 3.6 new backend architecture How to navigate among Views: How do I pass parameters between Views?

Posted on February 20, 2009 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

[This post is part of the developer's manual preview published on this blog. You can find temporary TOC here.]

 

Since Views are “disconnected” in a way and one switches them my navigating and not postback, this raises the issue of state. There is no ViewState and there is no ControlState. The question is how you pass information from one View to another.

 

New backend architecture works with two parameters:
  • Param - generic parameter used for transferring any given information between Views
  • ParentId - In modules where a hierarchy of data exists (e.g. blog is parent of the blog post, library is parent of the image or document) this parameter persists the id of the parent

 

Param



Param is used for carrying any kind of information from one View to another, with a twist, though. If you pass a parameter that can be converted to a GUID, on the next View you will be able to access GUID DataItemId property without parsing the actual parameter.

 

Let’s start with demonstrating some samples to see how to pass information between two views.

 

*** NOTE ***

 

Passing parameters slightly differs between Views based on ViewModeControl and ViewModeUserControl.

 

*** END NOTE ***

 

SAMPLE 1: Creating a command for a View and passing parameter that can be converted to GUID with it

 

Guid product1Id = new Guid("DFC2058E-C458-486c-A243-577528C51AA8"); 
this.EditProduct1.NavigateUrl = CreateHostViewCommand<ProductEditView>(product1Id.ToString()); 
 

Notice how you can overload a CreateHostViewCommand and pass parameter as the argument. You will receive this parameter on View to which you navigate (“ProductEditView”) and since the parameter can be converted to GUID, you’ll be able to access it as follows:

 

SAMPLE 2: Accessing received parameter which can be converted to GUID

 

Guid productId = this.DataItemId; 

 

The ability to automatically parse parameter as a GUIDs, does not mean that you can only pass GUIDs as parameters. We can send any kind of serializable data through parameters. Here is an example of sending a simple string text.

 

SAMPLE 3: Creating a command and passing a general type of string with it
 

NavigateHostCommand<ProductPreviewView>("Good morning!"); 

Now, on the View that will receive this parameter you can access its value as follows.

 

SAMPLE 4: Accessing the value of passed parameter which cannot be converted to a GUID

string message = HttpContext.Current.Request.QueryString[base.ParameterKey]; 
 

Notice, how we are using the ParameterKey as a QueryString key. By default the value of ParameterKey is “Param”, which means that in the url parameter will be transferred as follows:

 

www.mysite.com/sitefinity/admin/modules.aspx? module=StoreModule&route=StoreControlPanel.ProductsView.ProductEditView&Param=dfc2058e-c458-486c-a243-577528c51aa8

 

Alternatively, you can decide to override the ParameterKey property on your Views and replace it with something more specific and user friendly, such as “ProductID”.

 

*** WARNING ***

 

When overriding ParameterKey, make sure you override it on both View that sends parameter and the one that receives it. Otherwise, the receiving View will not be able to pick out the value of parameter from the QueryString.

 

*** END WARNING ***

 

ParentId


There are certain cases when you wish to persist some kind of context, while you use the generic parameter for other tasks. A good example of this scenario is Blogs module. Once user enters a blog, he continues to work with posts. So, for editing or previewing a post, generic parameter will be used to carry the id of the post. At the same time one would like to be able to have the information on the blog (parent) to which this post belongs at all times. This is where ParentId parameter comes in.

 

SAMPLE 5: Sending ParentId parameter to a view

 

Guid product1Id = new Guid("DFC2058E-C458-486c-A243-577528C51AA8"); 
Guid storeId = new Guid("8BB08866-E0AE-42c6-8DC3-6C59FF9039DD"); 
this.ViewProduct1.NavigateUrl = CreateHostViewCommand<ProductPreviewView>(product1Id.ToString(), storeId.ToString()); 
 

 

Built in feature of GUID parameter being automatically converted to Guid works for ParentId parameter as well. Let’s take a look at how would a receiving View access the values of the two parameters we have sent.

 

SAMPLE 6: Retriving the values of Param and ParentId on the receiving View

Guid productId = this.DataItemId; 
Guid storeId = this.ParentId; 
 

As for the Parameter, where one is able to override ParameterKey and change its value to something else, the same possibility exists for ParentId. One can override ParentIdKey property, which default value is “ParentID” and change it to something more specific, such as “Store”.

 

*** WARNING ***

 

When overriding ParentIdKey, make sure you override it on both View that sends parameter and the one that receives it. Otherwise, the receiving View will not be able to pick out the value of parameter from the QueryString.

 

*** END WARNING ***

 

Generally speaking, when navigating between Views, methods such as CreateHostViewCommand, NavigateHostViewCommand etc. will have overloads that accept Parameter and ParentId. By specifying those values, you will be sending the parameters to the receiving View.


progress-logo

The Progress Team

View all posts from The Progress Team on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments

Comments are disabled in preview mode.
Topics

Sitefinity Training and Certification Now Available.

Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.

Learn More
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Loading animation