Search code examples
asp.net-mvcasp.net-mvc-4linq-to-sqlmodels

How to pass any hyperlink or div id to another page in MVC?


here is my code to display images on the page

@foreach (var i in Model)
{
    <li>
        <a href="@i.Image" class="fancybox" data-fancybox-group="gallery" id="@i.Id">
            <img src="@i.Image" alt="" class="img-responsive">
        </a>
    </li>
}

in this code i want to get the id of clicked image hyperlink on the next page i want to display this image , and some other attributed based on id from database

any suggestions please


Solution

  • You can use the Url.Action helper method to build the url to another action method and pass the image id as a querystring parameter.

    <a href="@Url.Action("Details","Image",new {imageId=i.Id})" 
                           class="fancybox" data-fancybox-group="gallery" >
            <img src="@i.Image" alt="" class="img-responsive" />
     </a>
    

    When razor executes the above code, It will generate markup like this for the a tag.

    <a href=/"Image/Details?imageId=123"  
    

    Where 123 could the id of the image in your loop iteration.

    Assuming you have a Details action method in your ImageController which accepts the image id in a parameter named imageId

    public ActionResult Details(int imageId)
    {
      // imageId param will have the id of the clicked image. 
      // you can get the details of the image using the id and return something to the view.
      // To do : Return something
    }