.net foreach(模型中的var项),对象引用未设置为对象的实例
我使用控制器中的“列表视图”生成一个storeList.cshtml文件,在这个视图文件中,有一行:.net foreach(模型中的var项),对象引用未设置为对象的实例,.net,asp.net-mvc-5,.net,Asp.net Mvc 5,我使用控制器中的“列表视图”生成一个storeList.cshtml文件,在这个视图文件中,有一行: foreach (var item in Model) {} 当我运行应用程序时,这一行不断出现“Nullreference”错误和“Object reference not set to a instance of a Object”错误 我很困惑,因为: 1) 该视图文件是自动生成的;那么为什么这是不正确的呢 2) 我使用了完全相同的方式(“列表视图”)并生成了许多其他视图文件,但从未遇到
foreach (var item in Model) {}
当我运行应用程序时,这一行不断出现“Nullreference”错误和“Object reference not set to a instance of a Object”错误
我很困惑,因为:
1) 该视图文件是自动生成的;那么为什么这是不正确的呢
2) 我使用了完全相同的方式(“列表视图”)并生成了许多其他视图文件,但从未遇到过这个问题
<table class="uk-table">
<tr>
<th>
@Html.DisplayNameFor(model => model.CategoryID)
</th>
<th>
@Html.DisplayNameFor(model => model.typeID)
</th>
<th>
@Html.DisplayNameFor(model => model.name)
</th>
<th>
@Html.DisplayNameFor(model => model.storeUrl)
</th>
<th>
@Html.DisplayNameFor(model => model.storedes)
</th>
<th>
@Html.DisplayNameFor(model => model.tags)
</th>
<th>
@Html.DisplayNameFor(model => model.image)
</th>
<th>
@Html.DisplayNameFor(model => model.metaTitle)
</th>
<th>
@Html.DisplayNameFor(model => model.metaDescription)
</th>
<th>
@Html.DisplayNameFor(model => model.popular)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.CategoryID)
</td>
<td>
@Html.DisplayFor(modelItem => item.typeID)
</td>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.storeUrl)
</td>
<td>
@Html.DisplayFor(modelItem => item.storedes)
</td>
<td>
@Html.DisplayFor(modelItem => item.tags)
</td>
<td>
@Html.DisplayFor(modelItem => item.image)
</td>
<td>
@Html.DisplayFor(modelItem => item.metaTitle)
</td>
<td>
@Html.DisplayFor(modelItem => item.metaDescription)
</td>
<td>
@Html.DisplayFor(modelItem => item.popular)
</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 partial class store
{
public store()
{
this.Reviews = new HashSet<Review>();
}
public int id { get; set; }
public Nullable<int> CategoryID { get; set; }
public Nullable<int> typeID { get; set; }
public string name { get; set; }
public string storeUrl { get; set; }
public string storedes { get; set; }
public string tags { get; set; }
public string image { get; set; }
public string metaTitle { get; set; }
public string metaDescription { get; set; }
public Nullable<bool> popular { get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<Review> Reviews { get; set; }
public virtual Type Type { get; set; }
}
公共部分类存储
{
公共商店()
{
this.Reviews=newhashset();
}
公共int id{get;set;}
公共可空类别ID{get;set;}
公共可为空的typeID{get;set;}
公共字符串名称{get;set;}
公共字符串storeUrl{get;set;}
公共字符串storedes{get;set;}
公共字符串标记{get;set;}
公共字符串图像{get;set;}
公共字符串元标题{get;set;}
公共字符串元描述{get;set;}
公共可空的popular{get;set;}
公共虚拟类别{get;set;}
公共虚拟ICollection审阅{get;set;}
公共虚拟类型类型{get;set;}
}
错误
public ActionResult storeList()
{
return View();
}
对象引用未设置为对象的实例。
描述:执行期间发生未处理的异常
当前web请求的副本。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源
异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。
让我知道如何解决这个问题
谢谢您可以随意更改 模型中
public partial class store
{
public int id { get; set; }
public Nullable<int> CategoryID { get; set; }
public Nullable<int> typeID { get; set; }
public string name { get; set; }
public List<store> MyStoreList {get;set;}
}
公共部分类存储
{
公共int id{get;set;}
公共可空类别ID{get;set;}
公共可为空的typeID{get;set;}
公共字符串名称{get;set;}
公共列表MyStoreList{get;set;}
}
内部控制器
public ActionResult storeList()
{
store obj=new store();
obj.MyStoreList = new List<store>();// Load your list using uery
return View(obj);
}
public ActionResult存储列表()
{
store obj=新商店();
obj.MyStoreList=new List();//使用查询加载列表
返回视图(obj);
}
在cshtml中
@model projectname.Models.store
@foreach (var item in Model.MyStoreList) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.CategoryID )
</td>
<td>
@Html.DisplayFor(modelItem => item.typeID)
</td>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
</tr>
@model projectname.Models.store
@foreach(Model.MyStoreList中的var项){
@DisplayFor(modelItem=>item.CategoryID)
@DisplayFor(modelItem=>item.typeID)
@DisplayFor(modelItem=>item.name)
您可以根据需要更改此设置
模型中
public partial class store
{
public int id { get; set; }
public Nullable<int> CategoryID { get; set; }
public Nullable<int> typeID { get; set; }
public string name { get; set; }
public List<store> MyStoreList {get;set;}
}
公共部分类存储
{
公共int id{get;set;}
公共可空类别ID{get;set;}
公共可为空的typeID{get;set;}
公共字符串名称{get;set;}
公共列表MyStoreList{get;set;}
}
内部控制器
public ActionResult storeList()
{
store obj=new store();
obj.MyStoreList = new List<store>();// Load your list using uery
return View(obj);
}
public ActionResult存储列表()
{
store obj=新商店();
obj.MyStoreList=new List();//使用查询加载列表
返回视图(obj);
}
在cshtml中
@model projectname.Models.store
@foreach (var item in Model.MyStoreList) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.CategoryID )
</td>
<td>
@Html.DisplayFor(modelItem => item.typeID)
</td>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
</tr>
@model projectname.Models.store
@foreach(Model.MyStoreList中的var项){
@DisplayFor(modelItem=>item.CategoryID)
@DisplayFor(modelItem=>item.typeID)
@DisplayFor(modelItem=>item.name)
我将再次编辑我的问题,您需要设置模型视图(yourmodel);如下图所示,我在控制器中创建了我的表obj,以检查我是否从表中获取数据,以及它是否没有获取任何值。udara kasun视图(db.mymodel.tolist());这是我必须做的,我做了,它工作得很好。谢谢Mate,我会再次编辑我的问题,然后设置模型视图(yourmodel);像这样Matt,我在控制器中创建了我的表obj,以检查我是否从表中获取数据,并且它没有获得任何值。udara kasun视图(db.mymodel.tolist());这是我必须做的,我做了,它工作得很好。谢谢。你有想法吗?你有想法吗?