ListModel ActionResult () MVC

0

Personal I need a help to understand where I'm going wrong and how to fix a problem. I do not handle a lot of MVC, but I'm turning here.

I need to pass a Model class as a parameter to an ActionResult, but the variable is being sent empty.

My Index ()

 @model IEnumerable<Sistemas.Digital.Model.PoderesPJ.Poder>

@{
    ViewBag.Title = "Poderes";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@*@using (Html.BeginForm())
{
    *@<fieldset>
        <legend></legend>
        <h2>Poderes</h2>

        <table class="table">
            <thead>
                <tr>
                    <th>@Html.DisplayNameFor(model => model.Nome)</th>
                    <th>@Html.DisplayNameFor(model => model.Codigo)</th>
                    <th>@Html.DisplayNameFor(model => model.Inicio)</th>
                    <th>@Html.DisplayNameFor(model => model.Vencimento)</th>
                </tr>
            </thead>
            <tbody>                
                @foreach (var item in Model)
                {
                    <tr>                        
                        <td>@Html.DisplayFor(modelItem => item.Nome)</td>
                        <td>@Html.DisplayFor(modelItem => item.Codigo)</td>
                        <td>@Html.DisplayFor(modelItem => item.Inicio)</td>
                        <td>@Html.DisplayFor(modelItem => item.Vencimento)</td>
                        <td>@Html.ActionLink("Detalhes", "Detalhes", new { item = item.Detalhes })</td>
                   </tr>
                }
            </tbody>

        </table>
    </fieldset>

My method on the Controller

    public ActionResult Detalhes(PoderesPJ.Detalhes item)
    {
        return View(item);
    }

The problem I believe is in ActionLink, which does not send the completed Class.

Full Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sistemas.Digital.Model;
using Sistemas.Digital.GERAL;
using Util.Crawler;

namespace Aplicativos.CockPit.Controllers
{
    public class PoderController : Controller
    {
        //
        // GET: /Poder/

        List<PoderesPJ.Poder> listaPoder;
        Crawler cr = new Crawler();

        private List<PoderesPJ.Poder> ListaPoderes(string agencia, string conta, ref Crawler cr)
        {


            List<PoderesPJ.Poder> _poderes = new List<PoderesPJ.Poder>();

            ConsultaPoderesPJ consulta = new ConsultaPoderesPJ();
            _poderes = consulta.ConsultaPoderesAgenciaConta(ref cr, agencia, conta);

            return _poderes.OrderBy(d => d.Nome).ToList();
        }

        public ActionResult Index()
        {
            string _param1 = "1234";
            string _param2 = "456789";

                listaPoder = ListaPoderes(param1, param2, ref cr);
                return View(listaPoder);

        }

        public ActionResult Detalhes(PoderesPJ.Detalhes item)
        {
            return View();
        }

    }
}
    
asked by anonymous 21.02.2017 / 17:06

2 answers

1

I do not understand what you want to do with this:

    public ActionResult Detalhes(PoderesPJ.Detalhes item)
    {
        return View();
    }

Either way, it will not work. To drill down into the registry, you'd better pass the registry ID you want to see than the entire object:

    public ActionResult Detalhes(int id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        var poder = await db.Poderes.FindAsync(id);
        if (poder == null)
        {
            return HttpNotFound();
        }
        return View(poder);
    }

So bind can be done by ActionLink .

    
21.02.2017 / 17:29
-1

Srs.,

I was able to solve with a tip I received and Mendez's answer.

I filled the list with a Session Session["viewPoderes"] = listaPoder; , so I was able to use only the code to search for the item I need.

public ActionResult Detalhes(string cod)
{
    List<PoderesPJ.Poder> item = (List<PoderesPJ.Poder>)Session["viewPoderes"];
    var det = item.Where(d => d.Detalhes.Codigo == cod).FirstOrDefault();
    return View(det.Detalhes);
}

Thanks to everyone

    
21.02.2017 / 18:45