Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在cshtml中调用foreach循环时,对象引用未设置为对象的实例_C#_Linq_Model View Controller_Foreach_Tabular - Fatal编程技术网

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

C# 在cshtml中调用foreach循环时,对象引用未设置为对象的实例,c#,linq,model-view-controller,foreach,tabular,C#,Linq,Model View Controller,Foreach,Tabular,所以我对MVC和开发非常陌生。我试图做的基本上是创建一个用户的单行表视图,以便我可以编辑和更新该单行。(我希望我说的有道理) 所以我使用MVC Razor视图,我想创建一个表并在该表中进行编辑,而不使用MVC中已经内置的CRUD功能。我想对表格视图进行内联编辑 所以我创建了一个视图,它也有一个局部视图。我有一个模型和我的存储库。当我运行这个程序时,它会说Object没有设置为对象的实例,这是关于我的foreach循环。以下是数据。如果有人能提供帮助,我们将不胜感激。我是个新手,现在才真正开始编写

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

所以我使用MVC Razor视图,我想创建一个表并在该表中进行编辑,而不使用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>

}
@model ODT4.Models.catalogModel
@Html.Label(“目录”)
@Html.Label(“角色”)
@Label(“Is Admin”)
@Html.标签(“打印的证书”)
@Html.Label(“观看百分比”)
@Html.Label(“更新百分比”)
@Html.Label(“PI名称”)
@Html.Label(“操作”)
@foreach(Model.catalogatendes中的var行)//这是我获取错误消息的地方
{
{
RenderPartial(“目录行”,行);
}
}
局部视图

@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>

}
@model ODT4.Models.catalogModel
@{
@DisplayFor(m=>m.Catalog.CatalogCode)
@DisplayFor(m=>m.StudyRole)
@DisplayFor(m=>m.IsAdmin)
@DisplayFor(m=>m.IsTrainingDocPrinted)
@DisplayFor(m=>m.percentwasted)
@DisplayFor(m=>m.percentUpdateOn)
@DisplayFor(m=>m.PIName)
}
目录模型

 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();
    }
公共列表目录与会者{get;set;}
公共目录模型()
{
}
公共无效GetAttendesList(Guid id)
{
//CatalogAttendes=新列表();
CatalogAttendeeRepository=新建CatalogAttendeeRepository();
CatalogAttendes=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);

        }
公共列表getAttendeesAll()
{
返回db.Catalog_Attendee.ToList();
}
公共目录\u与会者getAttendeeByID(Guid id)
{
返回db.Catalog\u 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:             {
“/”应用程序中出现服务器错误。 ________________________________________ 对象引用未设置为对象的实例。 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。 异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。 源错误: 第17行: 第18行: 第19行:@foreach(Model.catalogAttendeers中的变量行) 第20行:{ 第21行:{
Model.catalogAttendes为空-因此出现异常

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


编辑:另外,您没有将模型返回到视图。返回PartialView(模型)

在“for”循环中实际使用“linq”查询之前,“linq”查询不会被处理。您确定查询中的“db.text”或子属性不为null吗?我刚刚在部分视图中返回了模型,现在错误消失了。-)db.text是我的数据库。我只是删除了表名并插入了文本,以省去实际的数据表名,我想这很愚蠢。我刚刚添加了return PartialView(model),它运行时没有错误。谢谢你听到它。请将答案标记为已接受以结束问题。