Load a type list into a View of another type

Asked

Viewed 1,083 times

1

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

So far I’ve got 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 ():

<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 why this missing reference to the Model ProfessionalSpecialization. I don’t know how to refer them.

  • I answered how to make the reference, but what mistake is really giving!?

2 answers

2


Within a select is not possible, but you can emulate this using a div and setting the style with the value overflow-y:auto;height:200px; that makes the div look like a select open. Therefore, I believe that some changes in your code can 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 the View and parse directly in the View, thus:

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 goes 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>

-1

Good afternoon.

To reference just use the following:

@using Aplicacao.Model.Class

You can reference on top of the standard model reference that you receive the data from Action:

@using Aplicacao.Model.Class
@model IEnumerable<Aplicacao.Model.Class>
  • @Using Manylife.ASP.Areas.Professional.Models.Professionalspecialization does not work either.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.