Load list of one type in a View of another type

1

I'm doing a professional registration and of course, my View is typed with this Model. However in the register I need to insert Specialization and Type (2 different models). These last 2 models need to come as lists and appear in checkboxes within a Select (HTML).

So far I have the following:

public ActionResult Create()
        {
            ProfessionalSpecializationDAO pSpecializationDAO = new ProfessionalSpecializationDAO();
            ViewBag.ListProfessionalSpecialization = pSpecializationDAO.ListProfessionalSpecialization(0);

            ProfessionalTypeDAO pTypeDAO = new ProfessionalTypeDAO();
            ViewBag.ListProfessionalType = pTypeDAO.ListProfessionalType(0);

            return View();
        }

View (HTML):

<div class="editor-field">
                    @{
                        ViewBag.ProfessionalSpecialization as List<ProfessionalSecialization>;

                        foreach (var item in ViewBag.ProfessionalSpecialization)
                        {                            
                            Html.CheckBoxFor(modelItem => item.Title);
                        }
                        Html.ValidationMessageFor(model => model.IdProfessionalSpecialization);
                    }
                </div>

Probably must be why it's missing reference to Model ProfessionalSpecialization . I do not know how to reference them.

    
asked by anonymous 08.05.2014 / 21:54

2 answers

2

Within a select it is not possible, but you can emulate this using a div and set style to overflow-y:auto;height:200px; which causes the div to look like a select open. So I think some changes to your code might solve the problem.

public ActionResult Create()
{
    ProfessionalSpecializationDAO psDao = new ProfessionalSpecializationDAO();

    return View(psDao.ListProfessionalSpecialization(0));
}

And the view looks like this at the beginning:

@model List<ManyLife.ASP.Areas.Professional.Models.ProfessionalSpecialization>

And soon after you complement:

<div class="editor-field" style="overflow-y:auto;height:200px;">
@{
    foreach (var item in Model)
    {                            
        Html.CheckBoxFor(modelItem => item.Title);
    }
}
</div>

If you want to use another model for some special reason, you can use ViewBag to pass the data to View and parse directly in View, like this:

public ActionResult Create()
{
    ProfessionalUser professional = new ProfessionalUser();
    professional.IdProfessionalSpecialization = 13;
    professional.Name = "Teacher";

    ProfessionalSpecializationDAO psDao = new ProfessionalSpecializationDAO();
    ViewBag.ListProfessionalSpecialization = psDao.ListProfessionalSpecialization(0);

    ProfessionalTypeDAO pTypeDAO = new ProfessionalTypeDAO();
    ViewBag.ListProfessionalType = pTypeDAO.ListProfessionalType(0);

    return View(professional);
}

And View looks like this:

@using ManyLife.ASP.Areas.Professional.Models
@model ManyLife.ASP.Areas.Professional.Models.ProfessionalUser

...

<div class="editor-field" style="overflow-y:auto;height:200px;">
<label>Especialização</label>
@{
    foreach (var item in (List<ProfessionalSpecialization>)ViewBag.ListProfessionalSpecialization)
    {                            
        Html.CheckBoxFor(modelItem => item.Title);
    }
    Html.ValidationMessageFor(model => model.IdProfessionalSpecialization);
}
</div>

...

<div class="editor-field" style="overflow-y:auto;height:200px;">
<label>Tipo de profissional</label>
@{
    foreach (var item in (List<ProfessionalType>)ViewBag.ListProfessionalType)
    {                            
        Html.CheckBoxFor(modelItem => item.Title);
    }
    Html.ValidationMessageFor(model => model.IdProfessionalSpecialization);
}
</div>
    
08.05.2014 / 22:56
-1

Good afternoon.

To reference just use the following:

@using Aplicacao.Model.Class

You can reference above the standard model reference that you receive Action data:

@using Aplicacao.Model.Class
@model IEnumerable<Aplicacao.Model.Class>
    
08.05.2014 / 22:02