Reputation: 555
I have been trying to pass parameters trough another page and this works, however i'm not getting what I desired and it has probably to do with what i pass.
The first thing i pass is a name but includes spaces and special character, the second thing i pass is a web link
how i send it:
<div class="col-sm-4">
<h3>Programming</h3>
@if (programming == null)
{
<p><em>Loading...</em></p>
}
else
{
foreach (var program in programming)
{
<a href="/CourseDetails/[email protected]">@program.Name</a>
<br />
}
}
</div>
where it goes to
@page "/CourseDetails"
@using Portfolio.Models;
@using Portfolio_Frontend.Data;
@using Microsoft.AspNetCore.WebUtilities
@inject NavigationManager NavigationHelper
<h3>CourseDetails</h3>
@if (Name == null)
{
<p><em>Loading...</em></p>
}
else
{
<p>@Name</p>
}
@code {
public string Name { get; set; }
protected override void OnInitialized()
{
var uri = NavigationHelper.ToAbsoluteUri
(NavigationHelper.Uri);
if (QueryHelpers.ParseQuery(uri.Query).
TryGetValue("name", out var name))
{
Name = name.First();
}
}
}
i tried parameters as well and now tried query string gives the same result. the name it should pass in this particular case is: C# Intermediate: Classes, Interfaces and OOP What i get is only 'C' I assume because it is not able to translate the #.
is there a way to pass literal strings?
where it goes to: https://localhost:5105/CourseDetails/?name=C#%20Intermediate:%20Classes,%20Interfaces%20and%20OOP
this seems right to me.
Upvotes: 1
Views: 9153
Reputation: 4281
I recommend letting go of the idea of navigating from page to page, and using components:
<div>
@if (SelectedItem is not null)
{
<MyResultsPage SelectedProgramClass=@SelectedItem />
}
</div>
@code
{
ProgramClass SelectedItem {get; set;}
void SomeWayToSelectMyItem(ProgramClass newSelection){
SelectedItem = newSelection;
StateHasChanged();
}
}
Then in your display page, MyResultsPage.blazor
<div>
<div>@SelectedProgramClass.name</div>
. . .
</div>
@code {
[Parameter]
ProgramClass SelectedProgramClass{get; set;}
}
<MyResultsPage
> will not show up in any way on the client, or even be initialized, until you've assigned something to SelectedProgramClass
.
Upvotes: 2
Reputation: 3861
Minor correction of URL syntax methodology
You have:
<a href="/CourseDetails/[email protected]">@program.Name</a>
Which has a URL of /CourseDetails/?name=C#
Normally, you would do either
/CourseDetails/C#
/CourseDetails?name=C#
Except, Blazor doesn't explicitly support optional route parameters (/CourseDetails?name=C#
)
It looks as though you can keep the optional query parameters and fiddle with the QueryHelpers.ParseQuery()
I don't quite buy into that but if you want to keep going that route check out this post by @chris sainty
Link: https://chrissainty.com/working-with-query-strings-in-blazor/
I would much rather create a new model (DTO) that knows exactly how to display the CourseDetails name in a URL encoded fashion for the link, and the display name for the user.
public class ProgramModel
{
private readonly string name;
public ProgramModel(string name)
{
this.name = name;
}
public string DisplayName => name;
public string RelativeUrl => HttpUtility.UrlEncode(name);
}
And when we need to render the links on the 'Courses' page, it would look like this:
@page "/courses"
@using BlazorApp1.Data
<div class="col-sm-4">
<h3>Programming</h3>
@foreach (var program in programming)
{
<a href="/CourseDetails/@program.RelativeUrl">@program.DisplayName</a>
<br />
}
</div>
@code {
public IEnumerable<ProgramModel> programming { get; set; }
protected override void OnInitialized()
{
programming = new List<ProgramModel>()
{
new ProgramModel("Rust Things"),
new ProgramModel("JavaScript Things"),
new ProgramModel("C# Things")
};
}
}
And finally, when displaying the CourseDetails page, we can simply decode the name from the URL with the same utility that encoded the string in the first place, instead of guessing whether or not it's the apps fault, or the browsers fault that the '#' is not getting encoded properly to '%23'
@page "/CourseDetails/{Name}"
@inject NavigationManager NavigationHelper
@using System.Web
<h3>CourseDetails</h3>
<p>@HttpUtility.UrlDecode(Name)</p>
@code {
[Parameter]
public string Name { get; set; }
}
Upvotes: 2