ASP.NET MVC 5: Link two models in the view

Asked

Viewed 487 times

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; }
}

Treinaviewmodel.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; }
}

Agendacontroller.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>
}

_Griddetrainings.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 cannot find a way to select the workouts, through the "Selected" checkbox, and associate these workouts to the schedule (Register.cshtml).

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

  • 1

    Didn’t you consider creating another View Model as a wrapper for your two? Since your view needs two models, I believe it makes sense to use a single that provides both.

  • The problem is that the view model is null.

No answers

Browser other questions tagged

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