gray
gray

Reputation: 133

C# mv5 pass parameter through link then get it in view

how to get the pass parameter in my view. i passed a parameter using

 <td>
<a  href="@Url.Action("report", "Map" , new { id = "Inventory"})" target="_blank"> Inventory Reports</a>
</td>

in my controller

public ActionResult report()
        {

            return View();
        }

../Map/report/Inventory

the url is already showing the pass data but how can i get it in jquery or javascript

Upvotes: 0

Views: 201

Answers (2)

AGB
AGB

Reputation: 2448

To use a parameter from your route you can pass it from your controller back to the page via a view model or just the viewbag if that's all you have:

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

In your view you can write the value to a hidden input

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

Then in your JavaScript you can read the id value and use it as you like

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

Upvotes: 2

Power Star
Power Star

Reputation: 1894

You can also use the lastIndexOf() function to locate the last occurrence of the / character in your URL, then the substr() function to return the substring starting from that location:

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

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

Upvotes: 1

Related Questions