简体   繁体   English

如何在不更改路由的情况下将参数从视图传递到控制器

[英]How can I pass a parameter from a view to a controller without having to change the routing

I am trying to have a series of buttons that call the same controller function but each passes a different parameter into the controller. 我试图让一系列按钮调用相同的控制器功能,但每个按钮都将不同的参数传递给控制器​​。

I have been looking around for a bit and most of the solutions say changing the routing config which I do not want to do. 我一直在寻找一些东西,大多数解决方案都说更改路由配置,这是我不想做的。 I am wondering if there is a different way to do it, or which is at least a standard practice. 我想知道是否有其他方法可以做到这一点,或者至少是一种标准做法。

<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();
}

I would also like to hide the string that is passing into the function but that is not a requirement. 我也想隐藏传递给函数的字符串,但这不是必需的。

According document at: https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.html.linkextensions.actionlink?view=aspnet-mvc-5.2 根据位于以下网址的文档: https : //docs.microsoft.com/zh-cn/dotnet/api/system.web.mvc.html.linkextensions.actionlink?view=aspnet-mvc-5.2

public static System.Web.Mvc.MvcHtmlString ActionLink (this System.Web.Mvc.HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes);

You need to note routeValues parameter. 您需要注意routeValues参数。

Just pass the custom value like this: 只需像这样传递自定义值:

@{
    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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM