2015-09-28 18 views
2

我有一個視圖,我在調用ActionResult方法,但在方法中放置一個斷點告訴我它沒有被調用。ActionResult不被調用

<div> 
<ul class="list-group"> 
    @foreach (var item in Model) 
    { 
     <li class="list-group-item"> 
      <h4>Slide ID: @item.SlideId</h4> 
      <p><i>Received: @item.TimeStamp</i></p> 
      <div class="row"> 
       <div class="col-md-4"> 
        <h4>@Html.ActionLink("View details", "Well", new {slideid = item.SlideId})</h4> 
        <img src="@Url.Action("Index", "Images", new {id = item.SlideId})"/> //This is where I want to call the method      
       </div> 
      </div> 
     </li> 
    } 
</ul> 

而這裏的方法:

public class ImagesController : Controller 
{ 
    // GET: Images 
    public ActionResult Index(string id) 
    { 
     byte[] imageData = new byte[0]; 
     string cs = "Data Source=" + "some path"; 

     using (SQLiteConnection con = new SQLiteConnection(cs)) 
     { 
      string stm = "SELECT LastImage FROM Well WHERE SlideId = " + "'" + id + "'"; 
      con.Open(); 

      using (SQLiteCommand cmd = new SQLiteCommand(stm, con)) 
      { 
       using (SQLiteDataReader rdr = cmd.ExecuteReader()) 
       { 
        while (rdr.Read()) 
        { 
         imageData = Serialize(rdr["LastImage"]); 
        } 

        rdr.Close(); 

       } 
      } 

      con.Close(); 
     } 
     return File(imageData, "image/png"); 
    } 

    public static byte[] Serialize(object obj) 
    { 
     var binaryFormatter = new BinaryFormatter(); 
     var ms = new MemoryStream(); 
     binaryFormatter.Serialize(ms, obj); 
     return ms.ToArray(); 
    } 
} 

我試圖實現與這個代碼是從數據庫中的圖像加載到該視圖。任何暗示我做錯了什麼?

與RouteConfig

現在:

public class RouteConfig 
{ 
    public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 
    } 
} 
+0

當你進入圖片網址時,你會看到什麼? – jrummell

+0

Id應該是整數。給參數另一個名字,如果你需要它是一個字符串。但是「Id」應該是整數 – blfuentes

+0

@jrummell然後我得到一個404. ActionResult Index()永遠不會被調用。 – Khaine775

回答

0

當你寫<img src="@Url.Action("Index", "Images", new {id = item.SlideId})"/>你不打電話的動作,但路線網址。結果是一個字符串,例如localhost:8080/images/index/abcd123456因此,如果要調用該操作,則需要使用@Html.Action("Index", "Images", new {id = item.SlideId})。注意@Html.Action而不是@Url.Action

+0

謝謝,現在該方法已被調用,但我得到一個「使用自定義TextWriter時OutputStream不可用」。 – Khaine775

+0

如果你想顯示圖像,你不想打電話給Html.Action – jrummell

0

我認爲,而不是打開和關閉每個圖像的數據庫連接,更好的方法是收集所有信息來呈現該頁面並將其發送到您張貼的視圖模型中。說它叫做HomeController的索引動作。它看起來就像是這樣的:

public class HomeController : Controller 
{ 
    public ActionResult Index(string id) 
    { 
     var listOfItems = new List<SomeClass>(); 

     string cs = "Data Source=" + "some path"; 
     using (SQLiteConnection con = new SQLiteConnection(cs)) 
     { 
      string stm = "SELECT SlideId, TimeStamp, LastImage FROM Well"; 
      con.Open(); 

      using (SQLiteCommand cmd = new SQLiteCommand(stm, con)) 
      { 
       using (SQLiteDataReader rdr = cmd.ExecuteReader()) 
       { 
        while (rdr.Read()) 
        { 
         var someItem = new SomeClass() 
         { 
          SlideId = rdr["SlideId"], 
          ImageData = Serialize(rdr["LastImage"]), 
          TimeStamp = rdr["TimeStamp"] 
         }; 
         listOfItems.Add(someItem); 
        } 

        rdr.Close(); 
       } 
      } 

      con.Close(); 
     } 

     return View(listOfItems); 
    } 
} 

當然,如果有太多的項目,你應該總是考慮分頁和限制列表中的項目的數量,以減少響應時間。

+0

你對[this]的意見是什麼(http://stackoverflow.com/questions/17952514/mvc-how-to-display-a- byte-array-image-from-model)metod添加圖像作爲base64字符串?如果我在你的回答中做了類似於你的事情,還有其他方法來呈現圖像,而不是將其轉換爲base64字符串嗎?它不適用於我,雖然它沒有渲染任何東西。 – Khaine775

+1

@ Khaine775:實際上,如果我正在實施此項目,我會將圖像存儲在AWS S3上,併發送圖像的URL而不是數據本身。這樣,您可以使用CDN的AWS CloudFront或類似的。如果你打算髮送圖像數據,我認爲我知道的最好方法是在Base64中進行編碼/解碼 –