C# 如何以URL格式将GET参数从一个cshtml页面传输到另一个cshtml页面?

C# 如何以URL格式将GET参数从一个cshtml页面传输到另一个cshtml页面?,c#,razor,C#,Razor,我的代码: start.cshtml: <a href="Page2.cshtml?parameter=one">link</a> get参数似乎没有从起始页转移到第2页,因为当我试图在第2页上显示“parameter”时,我得到的错误是它是空的 如何解决此问题?您可以创建一个链接: <%= Html.ActionLink("link", "Action", "Controller", new { parameter="one" }) %> 用于mvc

我的代码:

start.cshtml:

<a href="Page2.cshtml?parameter=one">link</a>
get参数似乎没有从起始页转移到第2页,因为当我试图在第2页上显示“parameter”时,我得到的错误是它是空的

如何解决此问题?

您可以创建一个链接:

<%= Html.ActionLink("link", "Action", "Controller", new { parameter="one" }) %>

用于mvc核心

<a asp-controller="Controller" asp-action="Action" asp-route-parameter="one" >link</a>
链接
然后您需要有一个操作接收此参数:

public async Task<IActionResult> Action(string parameter)
{
    ViewBag.parameter = parameter;
    return View()
}
<label>Parameter:</label>  @ViewBag.parameter 
公共异步任务操作(字符串参数)
{
ViewBag.parameter=参数;
返回视图()
}
在您的视图中,您只需显示此参数:

public async Task<IActionResult> Action(string parameter)
{
    ViewBag.parameter = parameter;
    return View()
}
<label>Parameter:</label>  @ViewBag.parameter 
参数:@ViewBag.Parameter
创建链接时:

<%= Html.ActionLink("link", "Action", "Controller", new { parameter="one" }) %>

用于mvc核心

<a asp-controller="Controller" asp-action="Action" asp-route-parameter="one" >link</a>
链接
然后您需要有一个操作接收此参数:

public async Task<IActionResult> Action(string parameter)
{
    ViewBag.parameter = parameter;
    return View()
}
<label>Parameter:</label>  @ViewBag.parameter 
公共异步任务操作(字符串参数)
{
ViewBag.parameter=参数;
返回视图()
}
在您的视图中,您只需显示此参数:

public async Task<IActionResult> Action(string parameter)
{
    ViewBag.parameter = parameter;
    return View()
}
<label>Parameter:</label>  @ViewBag.parameter 
参数:@ViewBag.Parameter

如果您想在没有助手类的情况下实现,那么下面是适合您的工作代码

syntax:
<a href="ControllerName/Action(or)PageName?parameter=one">link</a>

Example:
<a href="yourcontrollername/Page2?parameter=one">link</a>
语法:
例子:
注意:只需单独给出操作/视图名称,即page2,不应指定为page.cshtml

希望这将是有益的,请让我知道您的想法或反馈

谢谢
karthik

如果您想在不使用helper类的情况下实现,那么下面是适合您的工作代码

syntax:
<a href="ControllerName/Action(or)PageName?parameter=one">link</a>

Example:
<a href="yourcontrollername/Page2?parameter=one">link</a>
语法:
例子:
注意:只需单独给出操作/视图名称,即page2,不应指定为page.cshtml

希望这将是有益的,请让我知道您的想法或反馈

谢谢
karthik

这是mvc吗?通常在mvc中,你使用一个动作。我的教授说我应该在URL:Page2.cshtml?parameter=one中生成自己的链接。这是mvc吗?通常在mvc中,你使用一个动作。我的教授说我应该在URL:Page2.cshtml?parameter=one中自己生成链接。我在哪里可以找到我的控制器名?为什么不用“page2.cshtml”来指定它呢?我的教授说我应该在URL:Page2.cshtml?parameter=one中自己生成链接我在哪里可以找到我的控制器名?为什么不用“page2.cshtml”来指定它呢?我的教授说我应该在URL:Page2.cshtml?parameter=one中自己生成链接