Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html MVC 3视图:如何创建<;图像>;使用同时包含字符串和模型数据的src_Html_Asp.net Mvc 3_View_Model - Fatal编程技术网

Html MVC 3视图:如何创建<;图像>;使用同时包含字符串和模型数据的src

Html MVC 3视图:如何创建<;图像>;使用同时包含字符串和模型数据的src,html,asp.net-mvc-3,view,model,Html,Asp.net Mvc 3,View,Model,所以我在MVC3中开始了一个小家庭项目,在那里我需要制作一个照片库 我一直在遵循一个指南,在我的内容文件夹中创建了一个图像文件夹,我在其中放置了两张图片,还有一个xml文件,其中包含每个图片的路径和说明 然后将XML从文件系统加载到XDocument中并传递到视图 然后,视图应该遍历每个图像(有2个属性,路径和描述),并将此路径插入HTML src属性和a href属性中 我可以看到路径被正确地传递到视图中,例如img1.png。但是,我在获取工作路径和显示图片时遇到问题。我认为href和src

所以我在MVC3中开始了一个小家庭项目,在那里我需要制作一个照片库

我一直在遵循一个指南,在我的内容文件夹中创建了一个图像文件夹,我在其中放置了两张图片,还有一个xml文件,其中包含每个图片的路径和说明

然后将XML从文件系统加载到XDocument中并传递到视图

然后,视图应该遍历每个图像(有2个属性,路径和描述),并将此路径插入HTML src属性和a href属性中

我可以看到路径被正确地传递到视图中,例如img1.png。但是,我在获取工作路径和显示图片时遇到问题。我认为href和src无法正确解释属性字符串

或者可能是我需要一个相对路径或Url。内容编码

这里是风景

@foreach(var item in Model){
 <div>
   <a href="content/images/@item.path">
     <img src="content/images/@item.path"/>
   </a>
   <span>
    @item.description
   </span
</div>
@foreach(模型中的变量项){
@项目.说明

相对路径在此不起作用。因为路由路径。例如,您当前的url是
mydomain.com/Gallery/All
,您的图像路径为
content/images/@item.path
,它将返回
mydomain.com/Gallery/All/content/images/@item.path
,并且您的图像肯定不在该位置

为此,更简单的方法是像这样使用
Url.Content

@foreach(var item in Model){
 <div>
   <a href="@Url.Content("~/content/images/" + item.path)">
     <img src="@Url.Content("~/content/images/" + item.path)" />
   </a>
   <span>
    @item.description
   </span
</div>
@foreach(模型中的变量项){
@项目.说明

相对路径在此不起作用。因为路由路径。例如,您当前的url是
mydomain.com/Gallery/All
,您的图像路径为
content/images/@item.path
,它将返回
mydomain.com/Gallery/All/content/images/@item.path
,并且您的图像肯定不在该位置

为此,更简单的方法是像这样使用
Url.Content

@foreach(var item in Model){
 <div>
   <a href="@Url.Content("~/content/images/" + item.path)">
     <img src="@Url.Content("~/content/images/" + item.path)" />
   </a>
   <span>
    @item.description
   </span
</div>
@foreach(模型中的变量项){
@项目.说明