2016-04-22 3 views
0

こんにちは私が以前に行ったWebアプリケーションからこのコードを持っています。私はちょうどコピーして新しいものに貼り付けましたが、画像は新しいサイトに表示されません。私はパスを表示するコードを入力しても、それはidを表示するためにokを表示し、それはokと製品のすべてのプロパティを表示し、すべて表示されますがイメージは表示されません。いつ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; } 

    } 

} 

CATEGORYクラスであり、これはこれは、そのメソッドの参照を持っているCategoryControllerある

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; } 
    } 
} 

製品のクラスではあり に間違っているものを見つけることができません含まれています製品

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

     } 

NDこれは実際に

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

私は別のそれを持って、これと同じコードサイトの作業。 私はimagesフォルダ、ルートを再確認し、すべてがOKである

答えて

0

変更試してみよう:

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

これは働いたものです。しかし、なぜこれだけではなく、他のものでさえも他人でさえOKではない(明らかに) – shark6552

0

これにこの

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

私は今、私のPCから離れていますが、これはうまくいくと思います。しかし、これはsrc @ "item.ImagePath"とどう違うのですか? – shark6552

0

をあなたが指定するImagePath/番目の欠けていますrootと私はむしろstring変数を別の""から取り出すことになります。それはとにかく

0

<img src="~/@item.ImagePath" />

基本的に~記号を使用して、ルートパスに関連したファイルを参照することができます私のアプローチです。

関連する問題