Send complex view object to ASP MVC controller

Asked

Viewed 480 times

0

In my View I have a complex model where I display in a table only a few fields.

In this model I have another list called Tracks where I do not display its contents. Note the image below.

inserir a descrição da imagem aqui

I need to receive the object (var Item) which is my Albumviewmodel in the controller with all properties, namely: Band, Id, Image, Name, Releaseddate and Tracks.

In my controller the track list is coming empty:

inserir a descrição da imagem aqui

I wonder how I get the full object in my controller.

Thank you.

My View displaying the content of a Partial View

@model IsoFM.WebSite.Models.BandViewModel

@{
    ViewBag.Title = "Details";
}

<br />

<center><h2>Details Band</h2></center>

<div>

    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Name)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Name)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Image)
        </dt>

        <dd>
            <img src="@Html.DisplayFor(model =>model.Image)" style="height:200px;width:200px;" />
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Genre)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Genre)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Biography)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Biography)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.NumPlays)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.NumPlays)
        </dd>

    </dl>
</div>

<center><h2>Albums</h2></center>


@Html.Action("Details", "Album", new { idBand = this.Model.Id });



<p>
    @Html.ActionLink("Back to List", "Index", null, new { @class = "btn btn-primary" })
</p>

Controller da Partial View

using AutoMapper;
using IsoFM.Domain.Entities;
using IsoFM.Infra.Reporitory;
using IsoFM.WebSite.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace IsoFM.WebSite.Controllers
{
    public class AlbumController : Controller
    {
        AlbumRepository _albumRepository = new AlbumRepository();
        BandRepository _bandRepository = new BandRepository();
        // GET: Album
        [OutputCache(Duration = 600, VaryByParam = "none")]
        public ActionResult Index()
        {
            var viewModel = Mapper.Map<List<Album>, List<AlbumViewModel>>(_albumRepository.Obter());

            return View(viewModel);
        }

        [OutputCache(Duration = 600, VaryByParam = "idBand")]
        public ActionResult Details(string idBand)
        {
            if (idBand == null || string.IsNullOrEmpty(idBand) == true)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            if(Discografia.BandCollection == null)
                Discografia.BandCollection = Mapper.Map<List<Domain.Band>, List<BandViewModel>>(_bandRepository.ObterFull());

            var albumDetais = Discografia.BandCollection.Where(b => b.Id == idBand).FirstOrDefault().AlbumList;

            List<AlbumViewModel> listaAlbum = new List<AlbumViewModel>();

            foreach (var item in albumDetais)
            {
                listaAlbum.Add(item.FirstOrDefault());
            }

            return PartialView(listaAlbum);
        }

    }
}

Object of Partial View

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IsoFM.WebSite.Models
{
    public class AlbumViewModel
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Image { get; set; }
        public DateTime ReleasedDate { get; set; }
        public string Band { get; set; }
        public TrackViewModel[] Tracks { get; set; }
    }
}

Trackviewmodel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IsoFM.WebSite.Models
{
    public class TrackViewModel
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Duration { get; set; }        

    }
}

VIEW code where I need to pass the entire object to controller, as shown in image 1

@model IEnumerable<IsoFM.WebSite.Models.AlbumViewModel>

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Image)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ReleasedDate)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                <img src="@Html.DisplayFor(modelItem => item.Image)" style="height:200px;width:200px;" />
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ReleasedDate)
            </td>
            <td>
                @Html.ActionLink("Details", "Details", "Track", new { @id = item.Id, @Name = item.Name, @Image = item.Image, @ReleasedDate = item.ReleasedDate, @Band = item.Band, @Tracks = item.Tracks }, new { @class = "btn btn-primary" })
            </td>
        </tr>
    }

</table>

CONTROLLER Where I need to receive the View object Above!

using AutoMapper;
using IsoFM.Domain.Entities;
using IsoFM.Infra.Reporitory;
using IsoFM.WebSite.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace IsoFM.WebSite.Controllers
{
    public class TrackController : Controller
    {
        BandRepository _bandRepository = new BandRepository();
        [OutputCache(Duration = 600, VaryByParam = "*")]
        public ActionResult Details(AlbumViewModel model)
        {

            return View(model);
        }
    }
}
  • 1

    Include the code of your view, and the view model, avoid using code images.

  • I added the code snippet from View / Controller. I put the image to better illustrate!

  • missed the Albumviewmodel

  • I added the objects!

  • Could someone help me?

  • You posted your view view, not the form for creation or editing

  • Ready Leandro now is everything!

  • As I said before, you have view-only views, your problem is sending data from the view to the controller or controller to the view?

  • Send the View data to the Controller, I want to send the Albumviewmodel object to the Controller: Trackcontroller, with all the properties: Band, Id, Image, Name, Releaseddate and Tracks (Track Array).

Show 4 more comments
No answers

Browser other questions tagged

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