[英]MVC Null Reference on Model
我正在尝试实现局部视图,但模型遇到了麻烦。 我收到以下错误:
System.NullReferenceException: object reference not set to an instance of an object
检测到错误的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>
}
我真的不知道这是怎么回事,知道吗?
编辑:这是我的行动
public ActionResult _Index()
{
return PartialView(db.Revisiones.ToList());
}
@levelnis方法做到了!
Try using Html.Action instead of Html.Partial to render the partial.
这对我有用。
从Viewmodel中,您将返回List<BugTracker.Models.Revisiones>
List,并且视图中的Model类型也是正确的。
但是,您尝试访问的是ViewModel中的属性,而不是List迭代器中的属性。
我想,版本,描述,Fecha不是BugTracker.Models.Revisiones类的一部分。
解决方案是从ViewModel返回RevisonesViewModel
public ActionResult _Index()
{
return PartialView(object of RevisonesViewModel);
}
代替
public ActionResult _Index()
{
return PartialView(db.Revisiones.ToList());
}
您的模型是BUGTRACKER.Models.Revisiones的IEnnumerable。
当您进行迭代时,因为它是列表,所以没关系。
但是, 您不能执行以下操作:model.Version,因为它在模型中不存在。 您需要一个模型,该模型封装您的属性和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.