0

Im trying to retrieve data from my localDb in a cshtml file like this:

@Html.LabelFor(model => model.TIPOCLIENTE, "TIPO CLIENTE", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @{List<SelectListItem> listItems = new List<SelectListItem>();
              foreach (var item in Model.TipoClienteNamespace.TipoClienteValor)
              {
                  listItems.Add(new SelectListItem
                  {
                      Text =(string) item.Descricao
                  });
              }}
            @Html.DropDownList("TIPO CLIENTE", listItems,  new { @class = "form-control" }) 
            @Html.ValidationMessageFor(model => model.TIPOCLIENTE)
        </div>

But I get an error "Object reference not set to an instance of an object." on this line

foreach (var item in Model.TipoClienteNamespace.TipoClienteValor)

Anyone can help me?

freitas
  • 191
  • 4
  • 15
  • 1
    Either your `Model` or `Model.TipoClienteNamespace` is `null`. So before using them, check for null using `if` condition. – ramiramilu Jun 10 '15 at 19:09
  • They are used to in a insert field area like @Html.EditorFor(model => model.TELEFONE),but How can i retrieve some data store in a list and then show it in a dropdownlist? – freitas Jun 10 '15 at 19:51
  • What does your controller look like? Are you creating an instance of `Model.TipoClienteNamespace.TipoClienteValor` and then passing it to the view? – alex Jun 10 '15 at 20:40
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  Jun 11 '15 at 02:42
  • I just saw this link here: http://stackoverflow.com/questions/4319488/how-do-i-display-data-from-multiple-tables-in-a-single-mvc-view and it is exactly what i need.. I'll try something similar – freitas Jun 11 '15 at 11:34

0 Answers0