0

I'm having an issue that i can't quite understand. I define a model and am using it to create a form, two of it are lists that i would like to use to create checkbox fields. The problem is when I add values to those lists I get an error on defining the model instance.

Let me put some code to clear things.

My model class:

public class Recurso
{
    public int IDRecurso { get; set; }

    [Required(ErrorMessage = "Selecione um tipo de recurso.")]
    [Display(Name = "Tipo Recurso")]
    public int IDTipoRecurso { get; set; }

    public DateTime DataHora { get; set; }

    [Required(ErrorMessage = "Dê um titulo ao recurso.")]
    [Display(Name = "Titulo")]
    public string Titulo { get; set; }

    [Required(ErrorMessage = "Escreva uma breve descrição do recurso.")]
    [Display(Name = "Descrição")]
    public string Descricao { get; set; }

    [Required(ErrorMessage = "Adicione o ficheiro.")]
    [Display(Name = "Recurso")]
    public HttpPostedFileBase Ficheiro { get; set; }
    public string Mime { get; set; }

    [Display(Name = "Associar Recurso")]
    public int Associacao { get; set; }

    [Display(Name = "Tipos de Clientes")]
    public List<int> TiposClientes { get; set; }

    [Display(Name = "Clientes")]
    public List<int> Clientes { get; set; }
}

In the Controler:

public ActionResult AdicionarRecurso()
    {
        Recurso model = new Recurso();

        string email = this.GetEmailFromCookie();
        string key = Admin.GetUserKey(email);

        List<Entidades> clientes = Admin.GetAllClientes(email);
        foreach (Entidades cliente in clientes)
        {
            model.Clientes.Add(cliente.IDEntidade);
        }
        List<TiposClientes> tipos = Admin.GetTiposClientes(email);
        foreach (TiposClientes tipo in tipos)
        {
            model.TiposClientes.Add(tipo.IDTipoCliente);
        }

        return View(model);
    }

This gives me an Object reference not set to an instance of an object error in this line Recurso model = new Recurso();. But it works fine if I remove this part of the code:

List<Entidades> clientes = Admin.GetAllClientes(email);
foreach (Entidades cliente in clientes)
{
    model.Clientes.Add(cliente.IDEntidade);
}
List<TiposClientes> tipos = Admin.GetTiposClientes(email);
foreach (TiposClientes tipo in tipos)
{
    model.TiposClientes.Add(tipo.IDTipoCliente);
}

And I don't understand why.

Also I'm not even sure if it's possible to generate checkbox lists for the model lists Clientes and TiposClientes.

FabioG
  • 2,936
  • 3
  • 31
  • 50

1 Answers1

2

You need to instantiate the properties Clientes and TiposClientes on your model before calling Add() on those properties, as otherwise they are still null.

Try changing:

List<Entidades> clientes = Admin.GetAllClientes(email);
foreach (Entidades cliente in clientes)
{
    model.Clientes.Add(cliente.IDEntidade);
}
List<TiposClientes> tipos = Admin.GetTiposClientes(email);
foreach (TiposClientes tipo in tipos)
{
    model.TiposClientes.Add(tipo.IDTipoCliente);
}

to

List<Entidades> clientes = Admin.GetAllClientes(email);
model.Clientes = new List<int>(); // Added this line to instantiate the property
foreach (Entidades cliente in clientes)
{
    model.Clientes.Add(cliente.IDEntidade);
}
List<TiposClientes> tipos = Admin.GetTiposClientes(email);
model.TiposClientes = new List<int>(); // Added this line to instantiate the property
foreach (TiposClientes tipo in tipos)
{
    model.TiposClientes.Add(tipo.IDTipoCliente);
}
Chris
  • 3,210
  • 1
  • 33
  • 35
  • yeah! that's it. now I just need to figure out how to convert this values to a checkbox list and receive de selected ones in my model on submit. – FabioG Mar 19 '14 at 12:02
  • Try http://stackoverflow.com/questions/220020/how-to-handle-checkboxes-in-asp-net-mvc-forms (found by searching for `mvc list of checkboxes`) – Chris Mar 20 '14 at 10:27
  • Thanks but I'll have to use another aproach instead of checkboxes, because i've been told there'll be lists of 1000+ clients and checkboxes is not a viable way to achieve this. – FabioG Mar 20 '14 at 12:57