More in this section
Blogs RSS feed

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

by User Not Found

[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 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: 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.





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.




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.


Leave a comment
  1. Drew Mar 03, 2009
    Hey Ivan,

    Is it possible to pass multiple values in the QueryString?  For example, the id of the item which would result in the a guid and any other string value(s)?  (i.e."whatever"&Something="whatever")

  2. Drew Mar 03, 2009
    Hey Ivan,
    The above question refers to a "NavigateHostCommand" call.
  3. dido Mar 09, 2009
    Hi, Drew

    There are some restrictions of the length of the url, but, generally, any info can be passed if it is serialized and escaped.

    I believe it doesn't matter which command function you use for that matter.

    Plus, you can customize the key of both ParentId and Param, so it is pretty much customizable.

  4. Alex Mar 12, 2011
    Hello Ivan,

    I tried to find article how to pass multiple parameters to the URL. But  didn't find.
    I use method CreateHostViewCommand to redirect from my browse view to the edit view. But function CreateHostViewCommand  hasn't possibility to add multiple parameters. It only adds one parameter to the URL. Parameter name: "Param". 

    Could you help me and explain how can I pass multiple parameters to the URL and use for that CreateHostViewCommand method?

    The second problem I see is in CreateHostViewCommand. It builds incorrect LINK.
    I have the URL where extended parameter is presented. For example:

    The "ceId" is my additional parameter.

    When I call method to generate back to browse view link

    CreateHostViewCommand("TestListView", null, null)
    it returns me the link with parameter "ceId" and it value.


    Why method CreateHostViewCommand gets "ceId" parameter from the URl add it to the generated link?
    I thought the method should generates link like that:
     Look forward to hearing you.

    Leave a comment