1

I'm developing a school project using asp.net core with c#, my problem is when i click in a button i need to pass the data that i have inside my view to a new action. I'm using that form to send the data to my method

<form asp-action="PessoasCOunt" method="post">
        
   
    <input asp-for="Inicial" hidden/>
    <input asp-for="Final" hidden/>
    <input asp-for="AcessoVisita" hidden />
    <input asp-for="tempo" hidden/>
    <input asp-for="ApZona" hidden/>
      @foreach(var i in Model.ap)
        {
            <input asp-for="ap" hidden/>
        }

    
        @for(int i=0; i<Model.dados2.Count();i++)
        { 
            <input asp-for="dados2[i].ap_id" hidden/>
            <input asp-for="dados2[i].ap_name" hidden/>
            <input asp-for="dados2[i].numeroAcessos" hidden/>
            <input asp-for="dados2[i].year" hidden/>
            <input asp-for="dados2[i].MES" hidden/>
        <input asp-for="dados2[i].DIA" hidden/>
        
    }
    <div class="form-group"style="margin-bottom:2%;margin-top:2%;">
        <input type="submit" value="Consultar" class="btn btn-primary" />
    </div>
               
</form>

Thats the structure that i'm passing to the method.

public class dadosPassar
    {
        public List<Stored1>? dados2 { get; set; }
        public List<L_AccessPoint>? Aps { get; set; } = new List<L_AccessPoint>();
        public List<L_Zone>? Zones { get; set; } = new List<L_Zone>();
        public List<int>? ap { get; set; } 
        public DateTime Inicial { get; set; }
        public DateTime Final { get; set; }
        public string? AcessoVisita { get; set; }
        public string? tempo { get; set; }
        public string? ApZona { get; set; }
    }

And that method

public FileResult PessoasCOunt(dadosPassar dp)
        {
            //Retornar dados dos Aps escolhidos
            var objap = new List<L_AccessPoint>();
            if (dp.ApZona == "zonas")
            {
                for (int i = 0; i < dp.ap.Count(); i++)
                {
                    var objzones = _db.L_AccessPoint.Where(s => s.zone_id == dp.ap[i]).ToList();
                    for (int j = 0; j < objzones.Count; j++)
                    {
                        objap.Add(objzones[j]);
                    }
                }
                dp.Aps = objap;
            }
            else
            {
                //ds.Apzona é aps
                for (int i = 0; i < dp.ap.Count(); i++)
                {
                    var objzones = _db.L_AccessPoint.Where(s => s.ap_id == dp.ap[i]).ToList();
                    for (int j = 0; j < objzones.Count; j++)
                    {
                        objap.Add(objzones[j]);
                    }
                }
                dp.Aps = objap;
            }
            //criar stringBuilder para poder retornar o ficheiro
            StringBuilder sb = new StringBuilder();
            string filename = dp.AcessoVisita + dp.Inicial.ToString() + "_" + dp.Final.ToString() + ".csv";
            return File(System.Text.Encoding.Unicode.GetBytes(sb.ToString()), "text/csv", filename);
        }

When i select only 6 elements,the data arrives correctly to the method

1

2

But when i pass much data, the values arrives null

4

5

Thats the data that i'm passing.

6

Anyone know why is that happening?

  • Not sure, but you should try playing with RequestSizeLimit and DisableRequestSizeLimit decorators. – Mauricio Atanache May 02 '22 at 14:52
  • How many data elements are you passing when it comes back null, and are they all the same? – John May 02 '22 at 15:06
  • The data are all the same, just in diferent quantity. I update my question with the data that i'm passing. – Alexandre Lourenço May 02 '22 at 15:15
  • 1
    I use this: services.Configure(x => x.ValueCountLimit = int.MaxValue); Though you might not want to use maxvalue as I think it opens you up to a vulnerability. Also see this thread: https://stackoverflow.com/questions/38357108/form-submit-resulting-in-invaliddataexception-form-value-count-limit-1024-exce – pcalkins May 02 '22 at 19:02
  • @pcalkins tks for your help i'm going to check the question – Alexandre Lourenço May 02 '22 at 20:52
  • @pcalkins That option you said, probably could work for now but with the database growing up in one point that will stop working. – Alexandre Lourenço May 02 '22 at 21:00
  • you won't need to pass your whole database to the view (it's all hidden in your view anyway) .... not too sure what you are doing here, but remember that the data is already available on the server-side, so there's no need to pass it to a view and then back again. – pcalkins May 02 '22 at 22:46
  • I retrieve the data from the database in a controller called Index, then i pass that to the view to draw a file, and them i new to pass that data to my ```public FileResult PessoasCOunt(dadosPassar dp)``` method, i need the data that i pass from the controller to the view i need to pass that data to the ```public FileResult PessoasCOunt(dadosPassar dp)``` method. The data is already available on the server-side, I dont understand what you said with that phrase, how can i access that data that is available in the server side? @pcalkins – Alexandre Lourenço May 03 '22 at 08:42
  • the database is usually hosted by the server.... if this is a file upload, just process it on the server... no need to send all the data back to the view. (I think I'm not understanding the bigger picture though... can you describe in more general terms what you are trying to accomplish?) – pcalkins May 03 '22 at 17:40
  • I have the database from where i retrieve the data to the action index and use that data in the index view to draw a pivot table, and when i click a button i need to use the data that i had passed to the view in a new controller function. – Alexandre Lourenço May 03 '22 at 20:08

1 Answers1

0

So I could solve my problem by using a complete diferent way. To do that i used Session Variables to store the data the i retrieve from the database and i can use that in every action i want.