如何使用MVC将模型传递给视图?

时间:2019-06-15 03:14:54

标签: c# asp.net asp.net-mvc .net-core

.Net-Core是我的新手。我已经使用.Net大约3个月了。当我尝试将模型传递给视图时,出现一个错误,我的对象为null。有谁知道我为什么无法做到这一点?

在我的控制器中,我有:

public IActionResult ListPeople()
{
List<PersonModel> people = new List<PersonModel>();
people.Add(new PersonModel { FirstName = "Kim", LastName = 
"Smith", Age = 38 });
people.Add(new PersonModel { FirstName = "Bob", LastName = "Smith", Age = 38 });
people.Add(new PersonModel { FirstName = "Tim", LastName = "Smith", Age = 38 });
return View(people);
}

我的模型就是PersonModel类中的这些属性。

我认为我遇到了错误:

@model IEnumerable<practiceTimCorey.Models.PersonModel>

@{
ViewData["Title"] = "People";
}

<div class="text-center">
<h1 class="display-4">People</h1>
</div>
@Html.DisplayNameFor(model => model.FirstName)
@Html.DisplayNameFor(model => model.LastName)
@Html.DisplayNameFor(model => model.Age)
@Html.DisplayNameFor(model => model.IsAlive)

@foreach (var item in Model)
{

@Html.DisplayFor(modelItem => item.FirstName)
@Html.DisplayFor(modelItem => item.LastName)
@Html.DisplayFor(modelItem => item.Age)
@Html.DisplayFor(modelItem => item.IsAlive)



}

有人可以帮我吗?谢谢!

0 个答案:

没有答案