Linq @foreach模型为null:System.NullReferenceException:对象引用未设置为对象的实例
我在阅读@foreach上的观点时出现了这个错误 异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。模型为空 这是我的用户配置文件模型Linq @foreach模型为null:System.NullReferenceException:对象引用未设置为对象的实例,linq,asp.net-mvc-4,Linq,Asp.net Mvc 4,我在阅读@foreach上的观点时出现了这个错误 异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。模型为空 这是我的用户配置文件模型 public class UsersContext : DbContext { public UsersContext() : base("DefaultConnection") { } public DbSet<UserProfile> Use
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnection")
{
}
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<Secretarias> Secretarias { get; set; }
public DbSet<Secretarias_Direcciones> Secretarias_Direcciones { get; set; }
}
[Table("UserProfile")]
public class UserProfile
{
[Key]
public int UserId { get; set; }
[Required]
public string Nombre { get; set; }
[Required]
public int SecretariaId { get; set; }
[Required]
public int DireccionId { get; set; }
[Required]
public string UserName { get; set; }
}
这是我的看法
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Nombre)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.SecretariaId)
</td>
<td>
@Html.DisplayFor(modelItem => item.DireccionId)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.UserId }) |
@Html.ActionLink("Details", "Details", new { id=item.UserId}) |
@Html.ActionLink("Delete", "Delete", new { id=item.UserId })
</td>
</tr>
}
显然-检查用于在数据库中执行连接的每个ID的数据类型。还要确保数据库数据类型映射到正确的.Net数据类型
如果ID字段的数据类型看起来正常,我建议缩小导致错误的连接范围。修改LINQ表达式,使其具有单个联接,并确保没有错误。一次添加一个连接中的其他连接,直到出现错误 您得到的是
NullReferenceException
,因为您尚未向视图传递强类型模型。因此,在您的foreach
循环中,Model
是null
。通常的做法是:
return View(model);
真正的问题是你在这里混合了不同的概念。您试图以强类型的方式访问模型的属性,但却定义了一个ViewBag.model
属性,该属性是动态的,与视图中的model
属性没有任何关系
如果您想以强类型方式访问您的模型,并且我建议您这样做,那么您需要做一些更改。我建议您首先定义一个视图模型来表示UserProfile
的数据:
public class UserProfileViewModel
{
public int UserId { get; set; }
public string Nombre { get; set; }
public int SecretariaId { get; set; }
public int DireccionId { get; set; }
public string UserName { get; set; }
}
现在,更改操作以创建以下内容的列表:
public ActionResult ListaUsuarios()
{
using (UsersContext db = new UsersContext())
{
var model = from usrs in db.UserProfiles
select new UserProfileViewModel
{
UserId = usrs.UserId,
Nombre = usrs.Nombre,
UserName = usrs.UserName,
SecretariaId = usrs.SecretariaId,
DireccionId = usrs.DireccionId
};
return View(model.ToList());
}
}
注意,我在这里做了一些更改。首先,我使用语句添加了,以确保正确处理UsersContext
。接下来,我将查询更改为实例化UserProfileViewModel
s,而不是匿名类型。最后,我将model
作为参数直接传递给View()
,确保在UsersContext
处理之前调用ToList()
,以评估查询
最后,您只需对视图进行一次更改:
@model List<UserProfileViewModel>
@foreach (var item in Model)
{
// rest of code goes here
}
@型号列表
@foreach(模型中的var项目)
{
//剩下的代码在这里
}
@model
指令指定视图预期接收的确切模型类型,从而使其成为强类型。对于您的模型,请检查是否正在填充它。
根据发生的情况,您的模型可能只是空的
而且它的类型可能是人们所没有预料到的
--在控制器中,模型上下文引用上的foreach循环将允许您查看值I did single join,并检查表中的所有数据类型al all all all all int。不起作用。我再次编辑我的问题我做了更改,但现在我出现了以下错误{“无法隐式转换类型”System.Collections.Generic.List“到'Juareznlmvc.Models.UserProfile'”}@elmona更新了我的答案。抱歉,我在查询中出错。当我在标题表中添加此项时,John H不读取@Html.DisplayNameFor(model=>model.UserId)@elmona您想改为使用此项:@Html.DisplayNameFor(m=>item.UserId)
。
public ActionResult ListaUsuarios()
{
using (UsersContext db = new UsersContext())
{
var model = from usrs in db.UserProfiles
select new UserProfileViewModel
{
UserId = usrs.UserId,
Nombre = usrs.Nombre,
UserName = usrs.UserName,
SecretariaId = usrs.SecretariaId,
DireccionId = usrs.DireccionId
};
return View(model.ToList());
}
}
@model List<UserProfileViewModel>
@foreach (var item in Model)
{
// rest of code goes here
}