【问题标题】:Can not find the mistake with displaying images in my code在我的代码中显示图像时找不到错误
【发布时间】:2016-04-22 23:21:31
【问题描述】:

您好,我从以前的网络应用程序中获得了这段代码。我刚刚复制并粘贴到一个新站点,但图像未显示在新站点中。我什至输入代码来显示路径并显示正常,显示 id 并显示正常以及 Products 的所有属性,它们都显示正常但图像不显示。当 F5 我的应用程序运行正常时。但是当无法加载 img 时,它只显示小图标而不是图像。然而,当从浏览器使用检查器时,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; }
    }
}

这是具有包含产品的方法 Browse 的 CategoryController

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

这个相同的代码我在另一个网站上工作。 我仔细检查了图像文件夹,路线,一切正常

【问题讨论】:

    标签: c# asp.net-mvc html razor


    【解决方案1】:

    改成这样:

    <img src="@Url.Content("~/" + item.ImagePath)"/>
    

    【讨论】:

    • 这个行得通。但无法理解为什么这个而不是其他甚至另一个都可以(显然)
    【解决方案2】:

    试试这个

    <img src= "@Url.Content(item.ImagePath)" alt="Image" />
    

    【讨论】:

    • 我现在不在我的电脑旁,但我想这会起作用,但这与 src"@item.ImagePath" 有何不同
    【解决方案3】:

    &lt;img src="~/@item.ImagePath" /&gt;

    基本上~ 符号让您可以参考与根路径相关的文件。

    【讨论】:

      【解决方案4】:

      您在ImagePath 之前缺少/ 来指定root,我宁愿从另一个"" 中取出string 变量。无论如何,这就是我的方法

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多