如何将任何超链接或 div id 传递到 MVC 中的另一个页面?

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

这是我在页面上显示图像的代码

@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>
}

在这段代码中,我想获取下一页上单击的图像超链接的 ID 我想显示此图像,以及其他一些基于数据库中的 id 的属性

有什么建议吗

您可以使用 Url.Action 辅助方法将 url 构建到另一个操作方法并将图像 ID 作为查询字符串参数传递。

<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>

当razor执行上面的代码时,它会为a标签生成这样的标记。

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

其中 123 可以是循环迭代中图像的 id。

假设您的 ImageController 中有一个 Details 操作方法,它接受名为 imageId

的参数中的图像 ID
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
}