-1

I want to use an actionlink to call a controller. My URL is localhost:16252/Concert/Index/9.

I want to call create controller and send id (9) to the controller. How to access the id (9) from address bar by actionlink?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Ali Rezaii
  • 27
  • 4
  • What _exactly_ do you mean by "Access to the id(9) from address bar by actionlink"? You use `Html.ActionLink()` to print an URL to HTML, no address bar involved there. Are you asking how to read this ID in your controller code once someone clicks the action link? Anyway see http://stackoverflow.com/questions/8293934/passing-parameter-to-controller-action-from-a-html-actionlink – CodeCaster Jun 12 '15 at 09:43
  • @dev don't use `inline code` to highlight random terms. – CodeCaster Jun 12 '15 at 09:43
  • @CodeCaster thanks I will keep that in mind. – Dev Jun 12 '15 at 10:01
  • Id value in the address bar is not always 9 For example index/10 index/31 index/105 How to send id value to create control? – Ali Rezaii Jun 12 '15 at 12:24

2 Answers2

0

ActionLink has one overload that allows you to specify routevalues
MSDN Link here

@Html.ActionLink("LinkText", "Action", "Controller", new {Id= 9}, null)
Bobby Tables
  • 2,953
  • 7
  • 29
  • 53
  • Id value in the address bar is not always 9 For example index/10 index/31 index/105 How to send id value to create control? – Ali Rezaii Jun 12 '15 at 12:24
  • @AliRezaii that is clear but you can pass a varible there, and if you need more help we need to see your view – Bobby Tables Jun 12 '15 at 12:39
  • I Wrote this code ' @Html.ActionLink("تعریف طبقات", "Create", new { id = Request.QueryString["id"] }, new { @class = "css_addConcertLink" }) ' – Ali Rezaii Jun 12 '15 at 12:43
0

For Passing from View you have to use overload which takes parameter of RouteValueDictionary:

@Html.ActionLink("Link Text","MyAction", "My", new {id= 9},null) 

and in your controller:

public class MyController
{

    public ActionResult MyAction(int id)
    {
        // do something
        return View();
    }
}

Using this overload of Html.ActionLink()

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • Thought the same thing initially but i think that what you're using in this example is [this](https://msdn.microsoft.com/en-us/library/dd505070(v=vs.118).aspx) and the first param is the LinkText, and second is the Action so no controller there, – Bobby Tables Jun 12 '15 at 09:49
  • @Toby corrected , we have to use this overload:https://msdn.microsoft.com/en-us/library/dd493068%28v=vs.118%29.aspx – Ehsan Sajjad Jun 12 '15 at 10:01
  • Id value in the address bar is not always 9 For example index/10 index/31 index/105 How to send id value to create control? – Ali Rezaii Jun 12 '15 at 12:19
  • @AliRezaii you can pass variable there – Ehsan Sajjad Jun 12 '15 at 12:20
  • pass variable? I Wrote this code but it's not solution new{ id=request.querysreint["id"]} – Ali Rezaii Jun 12 '15 at 12:41