Model error on a razor page

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 get this error:

  

System.NullReferenceException: 'Object reference not set to an   instance of an object. '

That is, my model needs to be instantiated. How do I make the page work?

EDIT1

My controller (GetCities) 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();
        }
    }
}
    
asked by anonymous 09.08.2018 / 15:28

2 answers

0
  

That is, my model needs to be instantiated. How do I make the page work?

You can do a check on Action if the model is null, if so, instantiate it otherwise return your model.

public class CidadeController : Controller
{

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetCidades()
        {
            var model = db.Cidades.ToList();
            return View(model ?? new List<Cidade>());
        }
    }
}
    
09.08.2018 / 16:15
0

Your NullReferenceException might look like this:

@model IEnumerable<TreinamentoCrud.Models.Cidade>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

@if (Model.Count() == 0)
{
    Não há itens cadastrados
}
else
{
    <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>
}

Now before iterates it in View will be checked if it contains value

As seen by @Netinho, it is necessary that your foreach return the template. In his response, it treats it in Action , here, in another way of doing this, I'll treat in Action to display a message if it does not have items yet, its View would look like this:

public ActionResult GetCidades()
{
    return View(db.Cidades.ToList());
}
    
09.08.2018 / 15:49