C# 如何将参数从视图传递到控制器而不必更改路由
我尝试使用一系列按钮调用相同的控制器函数,但每个按钮都向控制器传递不同的参数 我已经四处寻找了一段时间,大多数解决方案都说我不想更改路由配置。我想知道是否有不同的方法,或者至少是一种标准做法C# 如何将参数从视图传递到控制器而不必更改路由,c#,asp.net-mvc,C#,Asp.net Mvc,我尝试使用一系列按钮调用相同的控制器函数,但每个按钮都向控制器传递不同的参数 我已经四处寻找了一段时间,大多数解决方案都说我不想更改路由配置。我想知道是否有不同的方法,或者至少是一种标准做法 <button class="button">Medium Voltage</button> @Html.ActionLink("Medium Voltage", "GaugeView", "PSA") [HttpGet] public ActionResult GaugeView
<button class="button">Medium Voltage</button>
@Html.ActionLink("Medium Voltage", "GaugeView", "PSA")
[HttpGet]
public ActionResult GaugeView(string LineName)
{
//Do stuff based on which LineName was specified
return View();
}
中压
@ActionLink(“中压”、“GaugeView”、“PSA”)
[HttpGet]
public ActionResult GaugeView(字符串行名)
{
//根据指定的LineName执行操作
返回视图();
}
我还想隐藏传递到函数中的字符串,但这不是一个要求。根据以下文档:
您需要注意routeValues参数。只需像这样传递自定义值:
@{
var url = Url.Action("GaugeView", "PSA", new { LineName = "value"});
}
<button onclick="window.location.href='@url';">Medium Voltage</button>
@Html.ActionLink("Medium Voltage", "GaugeView", "PSA", new { LineName = "value"}, null)
@{
var url=url.Action(“GaugeView”,“PSA”,新的{LineName=“value”});
}
中压
@ActionLink(“中压”、“GaugeView”、“PSA”、新的{LineName=“value”},null)
我如何才能将其放入按钮中?我不想让链接显示,我希望它是当按钮被点击时发生的事情。我已经尝试过了,但它仍然在控制器中显示为null的LineName。谢谢!这对传入参数起了作用。我是否能够为onclick使用我的按钮样式,或者我必须重新设置onclick的样式。我不太精通CSS/Html。没关系,我只需要给button类打电话。谢谢你的帮助!
@{
var url = Url.Action("GaugeView", "PSA", new { LineName = "value"});
}
<button onclick="window.location.href='@url';">Medium Voltage</button>
@Html.ActionLink("Medium Voltage", "GaugeView", "PSA", new { LineName = "value"}, null)