ASP.NET MVC 5: Linking two models to the view

1

I have the following scenario:

AgendaViewModel.cs

public class AgendaViewModel
{
    [HiddenInput(DisplayValue = false)]
    public int Id { get; set; }

    [HiddenInput(DisplayValue = false)]
    public int ClienteId { get; set; }

    // outras properties aqui...

    [HiddenInput(DisplayValue = false)]
    public virtual List<TreinoViewModel> Treinos { get; set; }
}

TreinoViewModel.cs

public class TreinoViewModel
{
    [HiddenInput(DisplayValue = false)]
    public int Id { get; set; }

    [StringLength(150), Required(ErrorMessage = "O nome é obrigatório.")]
    public string Nome { get; set; }

    // outras properties aqui

    [HiddenInput(DisplayValue = false)]
    public bool Selecionado { get; set; }
}

ScheduleController.cs

public class AgendaController
{
   ...
   public ActionResult Cadastrar()
   {
       return View();
   }

   [HttpPost]
   public ActionResult Cadastrar(AgendaViewModel agendaView)
   {
       ...
   }

   private void PreencherViewBags()
   {
       ViewBag.ListaTreinos = ObterTreinos(); // será utilizada em "_GridDeTreinos.cshtml"
   }
   ...
}

Register.cshtml

@model MeuProjeto.Presentation.ViewModels.Agenda.AgendaViewModel

@using (Html.BeginForm("Cadastrar", "Agenda", FormMethod.Post)) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <!-- Cliente -->
        <div class="form-group">
            @Html.Label("Cliente", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.ClienteId, (List<SelectListItem>)ViewBag.ListaClientes,
                new { htmlAttributes = new { @class = "form-control" } })
            </div>
        </div>

        <!-- demais campos aqui ... -->

        <!-- Grid de Treinos -->
        @Html.Partial("_GridDeTreinos")

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

_GridDeTreinos.cshtml

@model IEnumerable<MeuProjeto.Presentation.ViewModels.Treino.TreinoViewModel>
@using MeuProjeto.Presentation.ViewModels.Treino

<table id="tblTreinos" class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Nome)
        </th
        <th>
            @Html.DisplayName("Vincular")
        </th>
        <th>

        </th>
    </tr>

    @foreach (var item in (IEnumerable<TreinoViewModel>)ViewBag.ListaTreinos) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Nome)
            </td>
            <td>
                @Html.CheckBoxFor(modeItem => item.Selecionado, new { @id = item.Id.ToString() })
            </td>
            <td>
                @Html.ActionLink("Details", "Detalhes", new { treinoId = item.Id }) | 
            </td>
        </tr>
     }

</table>

I do not get a way to select the workouts, through the "Selected" checkbox, and associate these workouts with the agenda (Register.cshtml).

I would like to send to Action the Ids of each training and there I would do the association with the agenda. Or it would be better if you could already assign in the "agendaView" parameter, since it already has the training list.

    
asked by anonymous 24.11.2017 / 15:21

0 answers