Problems with CheckBoxFor and ListBoxFor [duplicated]

0

I need to render in my view a series of fields type checkbox (9 in total) where if one of them (the number 8) at the moment of clicking on it should appear a field type select , this last I already have it developed with jquery the theme of the animation to show and hide the div . The problem I have is in rendering the inputs in the view with razor , make the model and handle the data in the controller. All are static data.

I tried doing the following:

Model

This would be for fields of type checkbox

public class MiModeloCheckbox
{
    public MiModeloCheckbox()
    {
        this.Checkbox = new List<Checkbox>();
    }

    public List<Checkbox> Checkbox { get; set; }

    public class Checkbox
    {
        public bool IsSelected { get; set; }
        public int Codigo { get; set; }
        public string Nombre { get; set; }
    }
}

* This would be for the type field select

public class MiModeloSelect
{
    public List<SelectListItem> Autos { get; set; }
    public List<int> SelectedAutos { get; set; }
}

* Finally create a new model to join the previous two and be able to send them to view from my controller .

public class Input
{
    public MiModeloCheckbox MiModeloCheckbox { get; set; }
    public MiModeloSelect MiModeloSelect { get; set; }
}

In my controller I have the following

public async Task<ActionResult> Index()
    {
        // Logic

        Input input = new Input();
        input.MiModeloSelect.Autos = new List<SelectListItem>
        {
            new SelectListItem { Value = "1", Text = "AUTO 1" },
            new SelectListItem { Value = "2", Text = "AUTO 2" },
            new SelectListItem { Value = "3", Text = "AUTO 3" }
        };

        return View(input);
    }

But in this line input.MiModeloSelect.Autos gives me an exception just in Autos that says the following:

  

input.MiModeloSelect.Autos = 'input.MiModeloSelect.Autos' started an exception of type 'System.NullReferenceException'

In my view I occupy the following:

@model MiProyecto.Models.Input

@Html.ListBoxFor(m => m.MiModeloSelect.SelectedAutos, Model.MiModeloSelect.Autos)

I do not know how to solve this problem, someone who can help me?

    
asked by vcasas 05.06.2018 в 17:38
source

1 answer

1

When you make Input input = new Input(); you create an instance of Input , but the internal variables of the class remain at null if you do not initialize them. You have a couple of options:

  • Initialize it in the class constructor:

    public class Input
    {
        public MiModeloCheckbox MiModeloCheckbox { get; set; }
        public MiModeloSelect MiModeloSelect { get; set; }
    
        public Input()
        {
              this.MiModeloCheckbox = new MiModeloCheckbox();
              this.MiModeloSelect = new MiModeloSelect();  
        }
    }
    
  • Initialize it when creating the instance of the class manually:

    Input input = new Input();
    input.MiModeloCheckbox = new MiModeloCheckbox();
    input.MiModeloSelect = new MiModeloSelect();   
    
  • A couple of additional considerations:

  • It is very bad practice to call a property equal to its class, it leads to misunderstandings and makes it harder to read the code.

  • This problem will happen to you "recursively". I want to say that doing for example input.MiModeloSelect = new MiModeloSelect(); , you create an instance of MiModeloSelect , but both Autos as SelectedAutos will be null . That's why I recommend in each class to create a constructor and start the properties there as I put you in my first example.

  • answered by 05.06.2018 / 17:48
    source