C# 使用ASP.NETMVC4C从数据库获取渲染图像的视图#

C# 使用ASP.NETMVC4C从数据库获取渲染图像的视图#,c#,asp.net-mvc,image,asp.net-mvc-4,C#,Asp.net Mvc,Image,Asp.net Mvc 4,为了弄清楚这一点,我一整天都在拔我漏掉的一点头发 我有一个个人网络应用程序,我一直在编写各种教程,它进展得相当不错。我遇到的问题是,当我单击链接到强类型视图的图像时,视图账单显示为基本html,但我无法使用@Model.cardd从数据库中提取任何信息显示在页面上 这里是一些关于应用程序的信息。它基本上是一个交易卡片游戏中的卡片索引。主页工作得很好,我可以看到从数据库中提取的所有信息,包括图像。我已经链接了这些图片,点击图片将从数据库中提取身份并将其附加到url。无需任何操作,这将仅在浏览器页面

为了弄清楚这一点,我一整天都在拔我漏掉的一点头发

我有一个个人网络应用程序,我一直在编写各种教程,它进展得相当不错。我遇到的问题是,当我单击链接到强类型视图的图像时,视图账单显示为基本html,但我无法使用@Model.cardd从数据库中提取任何信息显示在页面上

这里是一些关于应用程序的信息。它基本上是一个交易卡片游戏中的卡片索引。主页工作得很好,我可以看到从数据库中提取的所有信息,包括图像。我已经链接了这些图片,点击图片将从数据库中提取身份并将其附加到url。无需任何操作,这将仅在浏览器页面中显示图像本身。这不是我想要的

我希望能够点击图像,并使用从数据库解析的数据打开一个视图,该视图与系统生成的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>
@model MTG\u Card\u App.Domain.Entities.Card
@如果(Model.ImageData!=null)
{
}
@型号.名称
@Model.CardText

@型号 @型号。颜色 @模型集

@URL.Action帮助程序在此页面上正常工作,并正确显示数据库中的图像。当鼠标悬停在图像上时,我会得到一个url,该url显示为localhost:*****/Card/FullImage?cardd=1,依此类推。当我单击图像时,我可以获得要加载的完整图像视图,但无法生成图像

下面是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 })" />
@model MTG\u Card\u App.Domain.Entities.Card
@{
ViewBag.Title=“FullImage”;
}
全像
我刚刚更新了所有这些信息并运行了应用程序,收到了一条以前从未收到过的新错误消息:

传入字典的模型项的类型为“System.Int32”, 但此词典需要类型为的模型项 “MTG_Card_App.Domain.Entities.Card”

在此方面的任何帮助都将不胜感激。提前感谢您提供的任何答案或信息,可以为我指明正确的方向


如果您需要查看其他文件,请告诉我。

您的问题来自以下代码:

public ViewResult FullImage(int cardId)
{
    Card card = repository.Cards.FirstOrDefault(p => p.CardID == cardId);
{
    return View(cardId); // throws InvalidOperationException
} ...(other closing brackets removed)
您没有将
Card
模型类从控制器返回到视图,而是尝试返回类型为
int
cardd
,因此抛出
invalidooperationexception
。将退货模式更改为
,即可正常工作:

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)

您看到的错误消息是因为您正在传递
返回视图(cardId)
,但您的FullImage视图声明它需要
@模型卡
。感谢您指出这一点。它修复了错误,现在可以100%工作。如果你想把它作为一个答案,我会更愿意为你接受它。