I have a property in a class, which contains a list of objects of another class, in which layer I fill the list?

1

Greetings, I have a class called Cv (Currículum vitae) and apart from the typical properties: Name, Surname, DNI ... it also contains a property "Driving licenses" which is a list of objects of the class "Driving licenses". Where do I have to generate the list and fill it out? I had thought within the Razor view itself (with strong typing), because in the end it is to generate some checboxs with each one of the cards. CODE:

Class clsCV:

    [Display(Name = "NIF")]
    [Required]       
    public string NIF { get; set; }

    [Display(Name = "Nom")]       
    public string Nom { get; set; }

    public List<clsCarnets> LlistaCarnets; //Lista de carnets la cual tiene un método preparado que me genera la lista y me la devuelve

    //Resto de propiedades

Controller:

    public class CVController : Controller
{      
    // GET: CV
    [HttpGet]
    public ActionResult CV()
    {
        return View();
    }

}

View (where you receive the strong typing of the clsCV class (currívulum vitae):

@model CurriculumVitaeMVC.Models.clsCv
@{
   Layout = null;
}
// Aquí formulario muy extenso con tipado fuerte --> Textboxfor, textareafor, etc... etc...
    
asked by Xavier 02.02.2016 в 17:29
source

2 answers

2

Here I am writing this simple example which you can run here: link

MODEL

Objects and lists must be declared in the model, sometimes the model is an entity framework entity or a DTO, example:

namespace Ejemplo
{
   public class CV
    {
      public string Nombre{get;set;}
      public List<CarnetDeConducir> CarnetsDeConducir{get;set;}
    }

  public class CarnetDeConducir
    {
      public int Id{get;set;}
      public bool Vigente{get;set;}
    }
}

CONTROLLER

Create intancias of the objects and fill the lists in the controller:

namespace Ejemplo
{
  public class HomeController : Controller
  {
    [HttpGet]
    public ActionResult Index()
    {
        CV cv=new CV()
        {
            Nombre="Xavier",
            CarnetsDeConducir=new List<CarnetDeConducir>()
            {
                new CarnetDeConducir(){Id=1,Vigente=true},
                new CarnetDeConducir(){Id=2,Vigente=false},
            }
        };
        return View(cv);
    }

  }
}

VIEW

And you use the View to link to the properties of the model (using Strongly Typed model):

@using Ejemplo
@model Ejemplo.CV
@{
 Layout = null;
 }

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <style type="text/css">


        table, th, td {
                       border: 1px solid black;
                      }
    </style>
</head>

<body>
    <div class="container">
        <span>Nombre</span>
        @Html.TextBoxFor(m=>m.Nombre)
        <div>
            <table>
                <tr><th>Id</th><th>Vigente</th></tr>
                @{int i=0;
                foreach(CarnetDeConducir cdc in Model.CarnetsDeConducir)
                {
                    <tr>
                        <td><span>@cdc.Id</span></td>
                        <td>@Html.CheckBoxFor(x=>@Model.CarnetsDeConducir[i].Vigente)</td>

                    </tr>
                    i++;
                }
                }
            </table>


        </div>
    </div>
</body>
</html>

As for the syntax I use to show the checkboxes, I base myself here: link

(Sorry for Spanglish)

    
answered by 02.02.2016 / 18:13
source
1

The data you use in the view you must generate from action in controller

public class HomeController : Controller{

    public ActionResult Index(){

       var model = new CvViewModel(){
            Nombre = ..
            CarnetConducir = new List<CarnetConducir>()
        }
    }

    return View(model);

}

As you will see it is from action the model is generated with the data that the view requires.

In the example, define the model in a simple way, of course in your case you would take the data from some repository.

    
answered by 02.02.2016 в 18:00