DoctorAV
DoctorAV

Reputation: 1189

How to access query string value in viewmodel?

I want to pass list of checked Id (checkedParcels) to another page to display list of details accordingly.

For this in first ViewModel I have implemented command on which execution I am able to navigate to another page. Here is that code:

Uri uri = new Uri("/UnitListForParcelPage?checkedParcel=" + checkedParcels,UriKind.Relative);
navigationService.NavigateTo(uri);

I am able to navigate to second page here is address as it shown in browser:

http://example.com/PropMgmtTestPage.aspx#/UnitListForParcelPage?checkedParcel=System.Linq.Enumerable+WhereEnumerableIterator%601%5BPropMgmt.ParcelServiceRef.Parcel%5D

My problem is I am using ViewModel to perform operation on this page but I am unable to find any method to access value passed through query string.

Update: On page code-behind I have added this code:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    string PSTR = NavigationContext.QueryString["checkedParcel"];
    MessageBox.Show(PSTR);
}

MessageBox is showing correct value now I just want to bind that to viewmodel property

I have used this approch to bind viewmodel to view:

<navigation:Page.Resources>
    <helpers:SimpleViewModelLocator ViewModelKey="UnitTransFormViewModel" x:Key="viewModelLocator"/>
</navigation:Page.Resources>
<navigation:Page.DataContext>
    <Binding Source="{StaticResource viewModelLocator}" Path="ViewModel"/>
</navigation:Page.DataContext>

Upvotes: 3

Views: 1278

Answers (2)

ovais
ovais

Reputation: 369

Another way Make a public Property and Assign it with the Value of checkedParcel and now you can use it in the ViewModel Cheers :)

UPDATE:

Just make a

public static string checkedParcel = string.Empty; 

in App.Xaml.cs

and When you call

App.checkedParcel = checkedParcels;
this.navigationService.NavigateTo(uri);

before that you need to give the Value to App.checkedParcel =checkedParcels;

and in your Navigated page mathod

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    string PSTR = App.checkedParcel;
    MessageBox.Show(PSTR);
}

Hope You Understand. You can Achieve this by Making a Property and Setting Accordingly.

Upvotes: 2

jv42
jv42

Reputation: 8593

You could use a broadcast service (like Messenger if you're using MVVM Light) to send the 'change view' notification, along with your parameter.

Then you'd have easily the View react (Navigate) and the ViewModel get its parameters.

Upvotes: 1

Related Questions