【问题标题】:Object reference not set to an instance of an object when calling foreach loop when calling it in the cshtml [duplicate]在cshtml中调用foreach循环时,对象引用未设置为对象的实例[重复]
【发布时间】:2015-03-04 22:04:46
【问题描述】:

所以我对 MVC 和开发非常陌生。我要做的基本上是创建用户的单行表视图,以便我可以编辑和更新该单行。 (我希望我说得通)

所以我正在使用 MVC Razor View,我想创建一个表并在该表中进行编辑,而不使用 MVC 中已经内置的 CRUD 功能。我想对表格视图进行内联编辑。

所以我创建了一个也有部分视图的视图。我也有一个模型和我的存储库。当我运行它时,它说 Object 未设置为对象的实例,它正在谈论我的 foreach 循环。这是下面的数据。如果有人可以提供帮助,那将不胜感激。我很新,现在才真正编码大约 3 个月。

控制器

public ActionResult Index(string id)
        {
            var attendee = (Guid)Membership.GetUser(id).ProviderUserKey;
            CatalogAttendeeModel model = new CatalogAttendeeModel();
            model.getAttendeesList(attendee);
            var catalog = from ca in db.text
                          join a in db.text on ca.textID equals   a.textID
                          where ca.textID== attendee
                          select ca;
            foreach (var item in catalog)
            {
                if (item.IsActive == null)
                {
                    item.IsActive = true;
                }
            }
            return PartialView(); 
        }

索引视图

@model ODT4.Models.CatalogAttendeeModel

<table>
    <thead>
        <tr>
            <th>
                @Html.Label("Catalog")
                @Html.Label("Role")
                @Html.Label("Is Admin")
                @Html.Label("Certificate Printed")
                @Html.Label("Percent Watched")
                @Html.Label("Percent Updated")
                @Html.Label("PI Name")
                @Html.Label("Action")
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var row in Model.catalogAttendees) //this is where I get the errormessage
        {
            {
                Html.RenderPartial("CatalogAttendeeSingleRow", row);
            }
        }
    </tbody>
</table>

局部视图

@model ODT4.Models.CatalogAttendeeModel

@{
    <div id = "crud_tableEdit" >
        <table>
            <tr>
                <td>@Html.DisplayFor(m => m.Catalog.CatalogCode)</td>
                <td>@Html.DisplayFor(m => m.StudyRole)</td>
                <td>@Html.DisplayFor(m => m.IsAdmin)</td>
                <td>@Html.DisplayFor(m => m.IsTrainingDocPrinted)</td>
                <td>@Html.DisplayFor(m => m.PercentWatched)</td>
                <td>@Html.DisplayFor(m => m.PercentUpdatedOn)</td>
                <td>@Html.DisplayFor(m => m.PIName)</td>
            </tr>
        </table> 

    </div>

}

目录模型

 public List<Catalog_Attendee> catalogAttendees { get; set; }

    public CatalogAttendeeModel()
    {

    }
    public void getAttendeesList(Guid id)
    {
        //catalogAttendees = new List<Catalog_Attendee>();
        CatalogAttendeeRepository rep = new CatalogAttendeeRepository();
        catalogAttendees = rep.getAttendeesAll();
    }

存储库

public List<Catalog_Attendee> getAttendeesAll()
        {
            return db.Catalog_Attendee.ToList();
        }

        public Catalog_Attendee getAttendeeByID(Guid id)
        {
            return db.Catalog_Attendee.FirstOrDefault(i => i.AttendeeID == id);

        }

这是错误信息:

Server Error in '/' Application.
________________________________________
Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

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

Source Error: 

Line 17:     </thead>
Line 18:     <tbody>
Line 19:         @foreach (var row in Model.catalogAttendees)
Line 20:         {
Line 21:             {

【问题讨论】:

  • 直到您在 'for' 循环中实际使用它时,才会处理 'linq' 查询。您确定查询中的“db.text”或子属性不为空吗?
  • 我刚刚在部分视图中返回了模型,现在错误消失了。 -)

标签: c# linq model-view-controller foreach tabular


【解决方案1】:

Model.catalogAttendees 为 null - 因此异常。

至于为什么......也许 linq 表达式没有返回它应该返回的内容。我没有看到 db.text 应该是什么,但我又没有看到所有代码。

编辑:另外,您不会将模型返回到视图中。返回部分视图(模型);

【讨论】:

  • db.text 是我的数据库。我只是删除了表格的名称并插入了文本,只是为了省略实际的表格名称,我想这很愚蠢。我刚刚添加了 return PartialView(model),它运行没有错误。谢谢你,麦肯
  • 很高兴听到这个消息。请将答案标记为已接受以关闭问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-12
  • 2012-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多