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.

2
  • I think it fails not in this lines Recurso model = new Recurso() Commented Mar 19, 2014 at 11:53
  • yup, that line is not the problem but that's the one that gives me the error when I open the page. Commented Mar 19, 2014 at 12:03

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

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.
Try stackoverflow.com/questions/220020/… (found by searching for mvc list of checkboxes)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.