2016-11-17 73 views
-1

我一直拉着我整天試圖弄清楚的那些小毛髮。獲取視圖以使用ASP.Net MVC 4呈現數據庫中的圖像C#

我有一個個人的網絡應用程序,我一直在遵循各種教程放在一起,它已經相當漂亮。我遇到的問題是,當我點擊一個鏈接到強類型視圖的圖像時,使用基本html顯示視圖條例顯示,但是我無法從數據庫中獲取任何信息以使用@ Model.CardID顯示在頁面上。

這是關於應用程序的一些信息。這基本上是來自交易卡遊戲的牌的索引。主頁面完美地工作,我可以看到從數據庫中提取的所有信息,包括圖像。自從我鏈接了這些圖像之後,點擊圖像將從數據庫中提取標識並將其附加到網址。如果不採取任何措施,只會在瀏覽器頁面中自行顯示圖片。這不是我想要的。

我希望能夠點擊圖像並使用從數據庫中解析出的數據打開一個與系統生成的URL關聯的視圖,對於我來說我無法想象它。

以下是控制器的動作,模型和視圖信息。爲控制器

使用陳述:

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using System.Web.Mvc; 
    using MTG_Card_App.Domain.Abstract; 
    using MTG_Card_App.Domain.Entities; 
    using MTG_Card_App.WebUI.Models; 

操作中,所述控制器:CardController.cs

public FileContentResult GetImage(int cardId) 
    { 
     Card card = repository.Cards.FirstOrDefault(p => p.CardID == cardId); 
    if (card != null) 
    { 
     return File(card.ImageData, card.ImageMimeType); 
    } 
    else 
    { 
     return null; 
    } 
} 

    public ViewResult FullImage(int cardId) 
    { 
     Card card = repository.Cards.FirstOrDefault(p => p.CardID == cardId); 
    { 
     return View(cardId); 
    } ...(other closing brackets removed) 

模型類:Card.cs

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.ComponentModel.DataAnnotations; 
    using System.Web.Mvc; 

    namespace MTG_Card_App.Domain.Entities 
    { 
    public class Card 
    { 
     [HiddenInput(DisplayValue = false)] 
     public int CardID { get; set; } 

     [Required(ErrorMessage = "Please enter a Card Name")] 
     public string Name { get; set; } 

     [DataType(DataType.MultilineText)] 
     public string CardText { get; set; } 

     [Required(ErrorMessage = "Please enter a Card Type")] 
     public string Type { get; set; } 

     [Required(ErrorMessage = "Please enter Card color")] 
     public string Color { get; set; } 

     [Required(ErrorMessage = "Please enter only one Card Set")] 
     public string Set { get; set; } 

     public byte[] ImageData { get; set; } 

     [HiddenInput(DisplayValue = false)] 
     public string ImageMimeType { get; set; } 
    } 
} 

摘要頁(這是卡片的索引)CardSummary.cshtml

@model MTG_Card_App.Domain.Entities.Card 

    <div class="item"> 

     @if (Model.ImageData != null) 
     { 
      <div> 
       <a href="@Url.Action("FullImage", "Card", new { Model.CardID })"> 
       <img width="105" height="150" src="@Url.Action("GetImage", "Card", 
       new { Model.CardID })" /> 
       </a> 
      </div> 

     } 

     <h2>@Model.Name</h2> 
     <p>@Model.CardText</p> 
     <h3>@Model.Type</h3> 
     <h3>@Model.Color</h3> 
     <p>@Model.Set</p> 

    </div> 

@ URL.Action助手在此頁面上正常工作,並正確顯示數據庫中的圖像。當鼠標懸停在圖像上時,我得到的URL顯示爲本地主機:*****/Card/FullImage?CardID = 1,以此類推CardSumnmary頁面中的每個圖像。當我點擊圖像時,我可以獲取FullImage視圖來加載,但無法獲取圖像生成。

下面是FullImage.cshtml

@model MTG_Card_App.Domain.Entities.Card 

    @{ 
     ViewBag.Title = "FullImage"; 
    } 
    <h2>Full Image</h2> 
    <img src="@Url.Action("GetImage", "Card", new { @Model.CardID })" /> 

我剛剛更新了所有這些信息,並跑了應用程序,收到我沒有得到一個新的錯誤消息的代碼:

傳入字典的模型項目類型爲'System.Int32', ,但該字典需要 'MTG_Card_App.Domain.Entities.Card'類型的模型項。

任何援助對此將不勝感激。預先感謝您提供的任何答案或信息,這些答案或信息可能會指向正確的方向。

請讓我知道是否有任何其他文件,你可能需要查看。

+0

您所看到的錯誤消息是因爲你傳遞'返回查看(CardId中)'但是你FullImage視圖聲明它需要一個'@model Card'。 – Jasen

+0

謝謝你指出。它修復了錯誤,現在它工作在100%。如果你想把它作爲答案,我會更願意接受它。 – amfilipiak

回答

0

您的問題來源於此代碼:

public ViewResult FullImage(int cardId) 
{ 
    Card card = repository.Cards.FirstOrDefault(p => p.CardID == cardId); 
{ 
    return View(cardId); // throws InvalidOperationException 
} ...(other closing brackets removed) 

返回從控制器Card模型類,你的看法相反的,你試圖返回cardId具有int類型,因此拋出InvalidOperationException。改變返回模型card並能正常工作:

public ViewResult FullImage(int cardId) 
{ 
    Card card = repository.Cards.FirstOrDefault(p => p.CardID == cardId); 
{ 
    return View(card); // returning model class, not a passed property id 
} ...(other closing brackets removed)