Raihan Ridoy
Raihan Ridoy

Reputation: 788

How to retrieve an actionlink text in a controller?

I have a list of actionlink in my navbar like this

  • @Html.ActionLink("Customer Profile", "Index", "Profile")
  • @Html.ActionLink("Supplier Profile", "Index", "Profile")
  • @Html.ActionLink("Publisher Profile", "Index", "Profile")
  • now i need to retrieve the link text in my controller to allocate some value in a property by comparing the value of the link text like this

     [HttpGet]
        public ActionResult Index()
        {
            Profile profile = new Profile();
            if(linktext=="Customer Profile")
            {
                profile.cust_supply_cat_id = 1;
            }
            else if (linktext == "Supplier Profile")
            {
                profile.cust_supply_cat_id = 2;
            }
            else if (linktext == "Publisher Profile")
            {
                profile.cust_supply_cat_id = 3;
            }
    
            return View(profile);
    
        }
    

    How can i do it?

    or if you get the idea what are the possible ways to do that??

    Upvotes: 0

    Views: 838

    Answers (2)

    Fakhar Ahmad Rasul
    Fakhar Ahmad Rasul

    Reputation: 1691

    you can send a value from Html.ActionLink like:-

    @Html.ActionLink("Customer Profile", "Index", "Profile", new { linktext: "Customer Profile", null })
    

    then receive the value in controller as a argument to a parameter like:-

    [HttpGet]
    public ActionResult Index(string linktext)
    {
        //your code
    }
    

    Upvotes: 1

    Michael Bakker
    Michael Bakker

    Reputation: 149

    You could add a parameter to the Index function. E.g:

    public ActionResult Index(int id).
    

    Then add a parameter value to your ActionLinks. E.g:

    @Html.ActionLink("Publisher Profile", "Index", "Profile", new {id = 1}, null)
    

    Hope this works for you.

    Upvotes: 1

    Related Questions