In Razor Pages, you can pass multiple parameter values to a page using the
@page
directive and the URL query string.Here's an example:
Suppose you have a Razor Page named MyPage.cshtml
and you want to pass two parameters to it: id
and name
. You can define the @page
directive in the page as follows:
@page "{id:int}/{name}"
In this example, id
is defined as an integer and name
is defined as a string. The int
suffix specifies that the value of id
must be an integer.
To pass values for these parameters, you can include them in the URL query string when navigating to the page. For example:
<a href="/MyPage/123/John">Go to MyPage</a>
In this example, the value of id
is 123 and the value of name
is "John". When the user clicks the link, they will be taken to the MyPage
Razor Page with the specified parameter values.
In the code-behind file for MyPage.cshtml
, you can access these parameter values using the RouteData.Values
collection. For example:
public class MyPageModel : PageModel
{
public int Id { get; set; }
public string Name { get; set; }
public void OnGet()
{
Id = (int)RouteData.Values["id"];
Name = (string)RouteData.Values["name"];
}
}
In this example, the OnGet
method retrieves the parameter values from the RouteData.Values
collection and assigns them to the Id
and Name
properties of the page model. These properties can then be used in the Razor Page to display dynamic content based on the parameter values.
Comments
Post a Comment