List<Model> Actionresult() MVC

Asked

Viewed 978 times

0

Guys, I need you to help me figure out where I’m going wrong and how to fix a little problem. I don’t know much about MVC, but I’m turning around 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 in Controller

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

The problem I believe is in Actionlink, which does not send the filled Class.

Controller Completo

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

    }
}

2 answers

1

I don’t understand what you want to do with it:

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

Either way, it won’t work. To detail the record, you’d better pass the Id of the record you want to see than the whole 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 the bind can be done by ActionLink.

-1

Gentlemen.,

I was able to solve it with a tip I received and Mendez’s reply.

I filled the list with a Session Session["viewPoderes"] = listaPoder;, this way I could 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);
}

Thank you all

  • This is bad practice. If the Session does not exist yet your code will have several different types of errors. It is better to program your code not waiting states (stateless), how to preach REST and MVC than to insist on an approach of Session.

Browser other questions tagged

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