C# .net强类型视图模型未设置为对象的实例

C# .net强类型视图模型未设置为对象的实例,c#,asp.net,.net,asp.net-mvc,razor,C#,Asp.net,.net,Asp.net Mvc,Razor,所以我创建了一个强类型视图。我的模型名为RestaurantReview.cs,如下所示: using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace OdeToFood.Models { public class RestaurantReview { public int Id { get; set; } publi

所以我创建了一个强类型视图。我的模型名为RestaurantReview.cs,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OdeToFood.Models
{
    public class RestaurantReview
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
        public int Rating { get; set; }
    }
}
@model IEnumerable<OdeToFood.Models.RestaurantReview>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.City)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Country)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Rating)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.City)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Country)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Rating)
        </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>
}

</table>
我让Visual Studio基于此创建了一个强类型列表模型,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OdeToFood.Models
{
    public class RestaurantReview
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
        public int Rating { get; set; }
    }
}
@model IEnumerable<OdeToFood.Models.RestaurantReview>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.City)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Country)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Rating)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.City)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Country)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Rating)
        </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>
}

</table>
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数

@ActionLink(“新建”、“创建”)

@DisplayNameFor(model=>model.Name) @DisplayNameFor(model=>model.City) @Html.DisplayNameFor(model=>model.Country) @DisplayNameFor(model=>model.Rating) @foreach(模型中的var项目){ @DisplayFor(modelItem=>item.Name) @DisplayFor(modeleItem=>item.City) @DisplayFor(modelItem=>item.Country) @DisplayFor(modelItem=>item.Rating) @ActionLink(“编辑”,“编辑”,新的{id=item.id})| @ActionLink(“详细信息”,“详细信息”,新的{id=item.id})| @ActionLink(“删除”,“删除”,新的{id=item.id}) }
当我运行站点时,我在“@foreach(模型中的var项)”行中得到一个空指针异常,突出显示模型对象,并声明“对象引用未设置为对象的实例”


因为我甚至没有写过,所以我并不真正理解这段代码是如何出错的,VisualStudio写过。这里发生了什么?

听起来好像您没有在
控制器中正确实例化您的模型

作为测试,您可以尝试以下方法:

public ActionResult Reviews()
{
   var model = new List<OdeToFood.Models.RestaurantReview>();
   model.Add(new OdeToFood.Models.RestaurantReview { Name = "Test" });
   model.Add(new OdeToFood.Models.RestaurantReview { Name = "Test2" });

   return View(model);
}
public ActionResult Reviews()
{
var模型=新列表();
添加(新的OdeToFood.Models.RestaurantReview{Name=“Test”});
添加(新的OdeToFood.Models.RestaurantReview{Name=“Test2”});
返回视图(模型);
}

但是,应该从数据库中正确填充模型。如果您可以粘贴控制器代码,这将有所帮助。

您的
控制器
显示已通过
餐厅视图
IEnumerable
。例如:

public class HomeController : Controller { //suppose this is your Home
    public ActionResult Index() {
        IEnumerable<OdeToFood.Models.RestaurantReview> model;
        model = from m in db.RestaurantReviews
                ... //your query here
                select m;
        return View(model); //pass the model here
    }
公共类HomeController:Controller{//假设这是您的家
公共行动结果索引(){
可数模型;
模型=从db.RestaurantReviews中的m开始
…//您的查询在这里
选择m;
返回视图(模型);//在此处传递模型
}

然后你将不会得到
null
exception

你能显示返回此视图的控制器操作代码吗?你的模型应该在那里实例化。你的
控制器
看起来怎么样?你是否从
控制器传递了任何
IEnumerable
?我知道我做了什么。我忘记在“返回视图”中包含参数()。“当我把它改为“返回视图(模型)”时,它起作用了。谢谢!@jimboweb great!;)是的,这是典型的问题。。。