Model error on Razor page

Asked

Viewed 202 times

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

  • PNET, take the Maccoratti course: http://www.macoratti.net/curso_aspnet_mvc5_basico.htm ... It’s cheap, it’s accessible, it’s in Portuguese.

2 answers

0

Missed the treatment to see if already values in Model, it is coming null, so NullReferenceException

Your View it may be so:

@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 you iterate on foreach will be verified if it contains value

As seen by @Netinho, it is necessary that in your Action back the model. In his answer, he treats it in Action, here, in another way to do this, I will treat at View to display a message if you don’t have items yet, your Action would look like this:

public ActionResult GetCidades()
{
    return View(db.Cidades.ToList());
}
  • This will not work if it is returning a null instead of returning a enumerable

  • Well seen, there was no attempt on me that he wasn’t returning anything.

0

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

You can do a check on Action if the model is null, if yes, instance it otherwise returns its model.

public class CidadeController : Controller
{

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

        public ActionResult GetCidades()
        {
            var model = db.Cidades.ToList();
            return View(model ?? new List<Cidade>());
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.