Send complex view object to ASP MVC controller

0

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

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

Ineedtogettheobject(varItem)whichismyAlbumViewModelinthecontrollerwithallproperties,ieBand,Id,Image,Name,ReleasedDateandTracks.

InmycontrollertheTracklistiscomingempty:

IwouldliketoknowhowIgettheentireobjectinmycontroller.

Thankyou.

MyViewdisplayingthecontentsofaPartialView

@modelIsoFM.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>

Partial View Controller

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

    }
}

Partial View Object

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 object

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 the 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 do I need to get the View object Up!

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);
        }
    }
}
    
asked by anonymous 26.02.2018 / 19:39

0 answers