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?