简体   繁体   English

MVC空参考模型

[英]MVC Null Reference on Model

I'm trying to implement a partial view but I'm having troubles with the model. 我正在尝试实现局部视图,但模型遇到了麻烦。 I'm getting the following error: 我收到以下错误:

System.NullReferenceException: object reference not set to an instance of an object

The HTML code where the error is detected is the following: 检测到错误的HTML代码如下:

@model IEnumerable<BUGTRACKER.Models.Revisiones>


<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.Version)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Descripción)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Fecha)
    </th>
    <th></th>
</tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Version)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Descripción)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Fecha)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
            @Html.ActionLink("Details", "Details", new { id = item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.Id })
        </td>
    </tr>
}

I really don't know what's going on here, any idea? 我真的不知道这是怎么回事,知道吗?

EDIT: This is my action 编辑:这是我的行动

public ActionResult _Index()
{
    return PartialView(db.Revisiones.ToList());
}

@levelnis approach did it! @levelnis方法做到了!

Try using Html.Action instead of Html.Partial to render the partial.

This worked for me. 这对我有用。

From the Viewmodel you are returning List<BugTracker.Models.Revisiones> List, in the view as well the type of Model is correct. 从Viewmodel中,您将返回List<BugTracker.Models.Revisiones> List,并且视图中的Model类型也是正确的。

But you are trying to access the properties that are in the ViewModel and not in the List iterator. 但是,您尝试访问的是ViewModel中的属性,而不是List迭代器中的属性。

Version, Description, Fecha are not part of BugTracker.Models.Revisiones class I suppose. 我想,版本,描述,Fecha不是BugTracker.Models.Revisiones类的一部分。

Solution would be to return the RevisonesViewModel from ViewModel 解决方案是从ViewModel返回RevisonesViewModel


    public ActionResult _Index()
    {
        return PartialView(object of RevisonesViewModel);
    }

instead of 代替


    public ActionResult _Index()
    {
        return PartialView(db.Revisiones.ToList());
    }

Your Model is an IEnnumerable of BUGTRACKER.Models.Revisiones 您的模型是BUGTRACKER.Models.Revisiones的IEnnumerable。

When you iterate it's ok since it's a list. 当您进行迭代时,因为它是列表,所以没关系。

However, you can not do this: model.Version since it doesNot exist in your model. 但是, 您不能执行以下操作:model.Version,因为它在模型中不存在。 What you need is a model that encapsulate your properties and a list of BUGTRACKER.Models.Revisiones. 您需要一个模型,该模型封装您的属性和BUGTRACKER.Models.Revisiones列表。

Public Class RevisonesViewModel{
      public string Version{ get; set; }
      public string Fetch{ get; set; }
      public string Description{ get; set; }
      public IEnumerable<BUGTRACKER.Models.Revisiones> MyRevisiones
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM