将模型值传递给图像中的href

本文关键字:href 图像 模型 值传 | 更新日期: 2023-09-27 18:03:02

我的Model有图像文件名。我想把文件名设置为href

@foreach (var images in Model)
{
    //href="~/ImageGallery/"@images.imgname 
    //which is href="~/ImageGallery/cat.jpg"
    <a href="~/ImageGallery/"  title="Banana" data-gallery>
        <img src="~/ImageGallery/thumbnails/" alt="Banana">
    </a>
}

怎么做?

将模型值传递给图像中的href

像这样:

@foreach (var images in Model)
      {
          <a href="~/ImageGallery/@(images.imgname)"  title="Banana" data-gallery>
          <img src="~/ImageGallery/thumbnails/" alt="Banana">
          </a>
      }

我更喜欢你使用Url.Content() helper,虽然上面的块也可以工作,但@Url.Content()从相对url生成正确的url:

@foreach (var images in Model)
          {
              <a href="@Url.Content("~/ImageGallery/"+images.imgname)"  title="Banana" data-gallery>
             <img src="~/ImageGallery/thumbnails/" alt="Banana">
              </a>
          }