在 cshtml 中调用 foreach 循环时,对象引用未设置为对象的实例

Object reference not set to an instance of an object when calling foreach loop when calling it in the cshtml

所以我对 MVC 和开发非常陌生。我想做的基本上是创建一个用户的单行 table 视图,以便我可以在该单行上进行编辑和更新。 (我希望我说得有道理)

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

所以我创建了一个也有部分视图的视图。我也有一个模型和我的存储库。当我 运行 这个时,它说 Object not set to an instance of an 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:             {

Model.catalogAttendees 为空 - 因此例外。

至于为什么...也许 linq 表达式没有return它应该的样子。我没有看到 db.text 应该是什么,但又一次我没有看到所有代码。

编辑:另外,您没有return将模型添加到视图中。 return 局部视图(模型);