Data Passing

We're well on the way to the completion of a web app that'll bring order to our chaotic hoard of thingamabobs. The next piece of the puzzle is data. The listing needs to know the page number we're on and the details needs to know the id of the selected thingamabob.

We can pass strongly-typed data using Navigation for ASP.NET. The forward and refresh navigation methods accept a second parameter holding this data. Back navigation provides no such overload because it remembers the data needed.

To select the 10th Thingamabob from the list we pass an id of 10.

StateController.Navigate("Select", new NavigationData { { "id", 10 } });
var link = StateController.GetNavigationLink("Select", new NavigationData {{ "id", 10 }});

To move to the second page of Thingamabobs we pass the page number 2.

StateController.Refresh(new NavigationData { { "page", 2 } });
var link = StateController.GetRefreshLink(new NavigationData { { "page", 2 } });

The Navigation for ASP.NET context object gives access to this passed data.

int page = (int) StateContext.Data["page"];

There are some bespoke classes for MVC and Web Forms that make passing navigation data even easier.


We can pass data in the return from a Controller using the ActionResult classes.

new NavigateResult("Select", new NavigationData { { "id", 10 } });
new RefreshResult(new NavigationData { { "page", 2 } });

We can pass data in a view using the HtmlHelper extension methods.

@Html.NavigationLink("text", "Select", new NavigationData { { "id", 10 } })
@Html.RefreshLink("text", new NavigationData { { "page", 2 } })

A navigation data ValueProvider lets us access the passed data as parameters in Controller methods.

public Thingamabob GetDetails(int id)

Web Forms

In .NET 4.5 we can pass data using ASP.NET HyperLink Controls.

<asp:HyperLink runat="server" NavigateUrl="{NavigationLink Select, id?int=10}"/>
<asp:HyperLink runat="server" NavigateUrl="{RefreshLink page?int=2}"/>

Before 4.5 we can pass data using NavigationHyperLink Controls.

<nav:NavigationHyperLink runat="server" Action="Select" ToData="id?int=10"/>
<nav:NavigationHyperLink runat="server" Direction="Refresh" ToData="page?int=2"/>

In .NET 4.5 a navigation data ValueProvider lets us access the passed data as parameters in data bound methods.

public Thingamabob GetDetails([NavigationData] int id)

Last edited Oct 11, 2014 at 2:11 PM by GrahamMendick, version 5