2
I have this cshtml
@model IEnumerable<TreinamentoCrud.Models.Cidade>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.nome)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.nome)
</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>
When I call the controller of this View, I have this mistake:
System.Nullreferenceexception: 'Object Reference not set to an instance of an Object.'
That is, my model needs to be instantiated. How do I work the page?
EDIT1
My controller (Getcidades) currently, but I will change to Netinho’s suggestion
public class CidadeController : Controller
{
// GET: GetCidade
public ActionResult Index()
{
return View();
}
public ActionResult GetCidades()
{
return View();
}
}
}
Use in your controller: var model = new List<City>(); More to better formulate an answer insert your controller into the question. This error happens due your model is null
– Netinho Santos
PNET, take the Maccoratti course: http://www.macoratti.net/curso_aspnet_mvc5_basico.htm ... It’s cheap, it’s accessible, it’s in Portuguese.
– William John Adam Trindade