嗨,我有这个代码从以前的一个网络应用程序,我做了。我只是复制和粘贴到一个新的,但图像没有显示在新的网站。我甚至输入代码来显示路径,它显示ok,显示id,它显示ok和所有来自产品的属性,它们都显示ok,但是图像不显示。当F5我的应用程序,网站运行良好。但是,当使用浏览器的检查器时,它只显示微小的图标,当img不能是loaded.Yet时,src分布有来自图像的正确路径。我找不到哪里出了问题,这是代码
这是类别类
namespace Mysite.Models
{
public class Category
{
//[ScaffoldColumn(false)]
public int CategoryID { get; set; }
[Display(Name = "Category Name")]
public string CategoryName { get; set; }
//[Display(Name = "Product Description")]
public string Description { get; set; }
public string ImagePath { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
}这是产品类
namespace Mysite.Models
{
public class Product
{
//[ScaffoldColumn(false)]
public int ProductID { get; set; }
[Required, StringLength(100), Display(Name = "Product Name")]
public string ProductName { get; set; }
[Required, StringLength(10000), Display(Name = "Product Description"), DataType(DataType.MultilineText)]
public string Description { get; set; }
public string ImagePath { get; set; } // Here is the image path that I want to show
public int? CategoryID { get; set; }
public virtual Category Category { get; set; }
}
}这是CategoryController,它有一个包含产品的方法Browse
namespace Mysite.Controllers
{
public class CategoriesController : Controller
{
private ProductContext db = new ProductContext();
public ActionResult Browse(string categories)
{
var categoriaModel = db.Categories.Include("Products")
.SingleOrDefault(c => c.CategoryName == categories);
return View(categoriaModel);/*(db.Categories.C)*/
}这是一个简化的视图,用来测试我想要在浏览器中显示的所有内容是否真的被显示了。
@model Mysite.Models.Category
@{
ViewBag.Title = "Browse";
}
<h2>Browse</h2>
@foreach(var item in Model.Products) {
<p>@item.ProductName , @item.ProductID , @item.ImagePath</p>
<img src="@item.ImagePath"/>
}但这正是它所展示的
Horizontal Faux Wood , 1 , Content/ProductImages/woodblind2.jpg /*small non-loaded picture icon here*/
Horizontal Real Wood , 2 , Content/ProductImages/woodblinds1.jpg /*small non-loaded picture icon here*/
Aluminium mini blinds , 10 , Content/ProductImages/aluminiumminiblind.jpg /*small non-loaded picture icon here*/同样的代码,我在另一个网站上也能用。我再次检查图片文件夹,路线和一切都好。
发布于 2016-04-22 14:53:02
将其更改为:
<img src="@Url.Content("~/" + item.ImagePath)"/>发布于 2016-04-22 14:51:08
尝尝这个
<img src= "@Url.Content(item.ImagePath)" alt="Image" />发布于 2016-04-22 14:58:59
您在ImagePath之前缺少了th ImagePath来指定root,我宁愿将string变量从另一个""中删除。不管怎样,这就是我的方法
https://stackoverflow.com/questions/36796893
复制相似问题