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

时间:2014-05-12 17:02:42

标签: c# asp.net-mvc-4

我的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>
}

怎么做?

1 个答案:

答案 0 :(得分:4)

像这样:

@foreach (var images in Model)
      {

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

我希望你使用Url.Content()帮助器虽然上面的块也可以,但@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>
          }