数据库设置不正确,IList上的值为空

过度

我已经将来自api调用的数据保存到数据库中,但是当尝试将数据打印到页面时,我得到的对象引用未设置为对象实例错误:对象引用未设置为对象实例。我正在尝试访问卡详细信息视图上的image_url属性,但是它一直在中断

我的模特:

public class Card
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int? Atk { get; set; }
        public int? Def { get; set; }
        public string Desc {get; set;}
        public int? Level { get; set; }
        public string Type { get; set; }
        public string Attribute { get; set; }
        [DisplayName("Image")]
        public IList<Image> Card_Images { get; set; }

        public IList<Deck> Deck { get; set; }
    }

 public class Image
    {
        public int Id { get; set; }
        public string image_url { get; set; }
        public string image_url_small{ get; set;  }
    }

我的控制器动作:

public ActionResult Details(int id)
        {
            var card = _context.Cards.SingleOrDefault(c => c.Id == id);

            if (card == null)
                return HttpNotFound();

            return View(card);
        }

我的数据库表等(如VS Server Explorer中所示):

Cards
    Id (key sign here)
    Name
    Atk
    Def
    Desc
    Level
    Type
    Attribute

Images
    Id (key sign here)
    image_url
    image_urul_small
    Card_Id

我的观点:

@model YGOBuilder.Models.Card

<div>
    <h4>Card</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Name)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Name)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Atk)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Atk)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Def)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Def)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Desc)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Desc)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Level)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Level)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Type)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Type)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Attribute)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Attribute)
        </dd>

        <dt>
            @Html.DisplayFor(model => model.Card_Images)
        </dt>

        @foreach (var item in Model.Card_Images)
        {
            foreach (var i in item.image_url)
            {
                <td>
                    <img src=@i height="300" width="200">
                </td>
            }

        }



    </dl>
</div>
<p>
    @Html.ActionLink("Back to List", "Index")
</p>

我试图通过连接到我的模型的image_url属性来显示在视图上,但似乎在这里绊倒了:

 @foreach (var item in Model.Card_Images)
加布里埃尔·卢西(Gabriel Luci)

根据文档,关系应声明为virtual List<>我怀疑IList仍然可以工作,但是virtual更重要。

而且,它还需要知道如何联接表,因此您需要名称匹配的ID属性。

public class Card
{
    public int CardId { get; set; }
    public string Name { get; set; }
    public int? Atk { get; set; }
    public int? Def { get; set; }
    public string Desc {get; set;}
    public int? Level { get; set; }
    public string Type { get; set; }
    public string Attribute { get; set; }
    [DisplayName("Image")]
    public virtual List<Image> Images { get; set; }

    public virtual List<Deck> Decks { get; set; }
}

public class Image
{
    public int ImageId { get; set; }
    public int CardId { get; set; }
    public string image_url { get; set; }
    public string image_url_small{ get; set;  }
}

注意ImageIdCardId和类中的添加CardId属性Image这样便知道哪个Image属于哪个Card

您可能需要在Deck课堂上做同样的事情

您也正在循环播放item.image_url,这是一个string结果是您遍历了字符串中的每个字符。您不需要该内部循环。只需item.image_urlimg标记中使用

@foreach (var item in Model.Card_Images)
{
    <td>
        <img src="@item.image_url" height="300" width="200">
    </td>
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章