【问题标题】:C# mv5 pass parameter through link then get it in viewC# mv5 通过链接传递参数然后在视图中获取它
【发布时间】:2018-01-17 02:29:04
【问题描述】:

在我看来如何获取pass参数。 我使用

传递了一个参数
 <td>
<a  href="@Url.Action("report", "Map" , new { id = "Inventory"})" target="_blank"> Inventory Reports</a>
</td>

在我的控制器中

public ActionResult report()
        {

            return View();
        }

../地图/报告/库存

url 已经显示了传递数据,但是我如何在 jquery 或 javascript 中获取它

【问题讨论】:

  • 我认为你使用了public ActionResult report(string id)(id = URL 参数)。当控制器返回视图后你期望什么?
  • 我只需要在javascript中获取pass值。
  • 使用 ViewBag/ViewData 并像这样放入 JS:var id = '@ViewBag.Id'(或使用视图模型)。
  • 谢谢它对我有用。

标签: javascript c# asp.net-mvc-5


【解决方案1】:

要使用路由中的参数,您可以通过视图模型或仅通过视图包将其从控制器传递回页面:

public ActionResult report(string id)
{
  ViewBag.id = id;
  return View();
}

在您看来,您可以将值写入隐藏输入

@Html.Hidden("id", ViewBag.id)

然后在您的 JavaScript 中,您可以读取 id 值并随意使用它

var id = document.getElementsByName("id")[0].value;

【讨论】:

    【解决方案2】:

    您还可以使用 lastIndexOf() 函数定位 URL 中最后一次出现 / 字符,然后使用 substr() 函数返回从该位置开始的子字符串:

    var value =this.href.substring(this.href.lastIndexOf('/') + 1);
    

    alert(window.location.href.substring(window.location.href.lastIndexOf('/') + 1))

    【讨论】:

      猜你喜欢
      • 2017-12-17
      • 2021-12-12
      • 2015-04-04
      • 1970-01-01
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 2021-10-16
      • 2012-10-18
      相关资源
      最近更新 更多