'I have a value that i retrieve from a view and i have to pass that value through 2 views, but that is passing a null value to the variabel asp.net

I'm developing a scholl project and i need to pass the variable ApZona retrieved in the view Filtros1

        <form asp-action="Filtros">
            <center>
                <div  style="margin-bottom:2%;">
                        <p>Filtrar entre APs ou Zonas</p>
                        <div style="margin-bottom:2%; width:70%; text-align:center; text-align-last:center ">
                        <select asp-for="ApZona" class="form-control">
                            <option value="null"> Escolha AP´s ou zonas</option>
                            <option value="aps">  AP's </option>
                            <option value="zonas"> Zonas </option>
                        </select>
                        </div
                </div>
                <div class="form-group"style="margin-bottom:2%;margin-top:2%;">
                    <input type="submit" value="Consultar" class="btn btn-primary" />
                </div>
            <center>
        </form>

, and pass that variable ApZona to the view Filtros(that arrives correctly),

<form asp-action="Index">
            <center>
           <div class="form-group">
               <div style="margin-bottom:2%; margin-top:2%;">
                    <p>Introduza a data inicial </p>
                    <input id="Inicial" type="datetime-local" name="Inicial" />
                </div>
                <div  style="margin-bottom:2%;" >
                <p>Introduza a data final </p>
                <input id="Final" type="datetime-local" name="Final" />
                </div>
                <div  style="margin-bottom:2%;">
                    <p>Acessos ou Visistas</p>
                    
                    <div style=" width:70%; text-align-last:center">
                    <select asp-for="AcessoVisita" class="form-control">
                        <option value="null">Escolha uma opção para o filtro</option>
                        <option value="sessao">Número de Sessoes</option>
                        <option value="visitas">Número de Visitas</option>
                    </select>
                    </div>
                </div>
                <div  style="margin-bottom:2%;">
                    <p>Dia/Mes/Ano</p>
                    <div style=" width:70%; text-align-last:center;margin-bottom:2%;">
                    <select asp-for="tempo" class="form-control">
                        <option value="null"> Escolha uma opção para o filtro </option>
                        <option value="day"> Dia </option>
                        <option value="month"> Mês </option>
                        <option value="year"> Ano </option>
                    </select>
                    </div
                </div

                 
                @if(Model.ApZona == "aps")
                    {
                        <div  style="margin-bottom:2%;">
                            <p>Escolha os AP's que quer verificar</p>
                            <div style="margin-bottom:2%; width:70%; text-align:center; text-align-last:center ">
                                <select asp-for="ap" class="form-control">
                                    <option value="null"> Escolha um Ap ou todos</option>
                                    <option value="todos"> Todos os AP's' </option>
                                    @foreach(var i in Model.Aps)
                                    {
                                        <option [email protected]_id> @i.ap_name </option>
                                    }
                                </select>
                            </div
                        </div>  
                    }
                else
                {
                    <div  style="margin-bottom:2%;">
                        <p>Escolha os AP's que quer verificar</p>
                        <div style="margin-bottom:2%; width:70%; text-align:center; text-align-last:center ">
                            <select asp-for="ap" class="form-control">
                                <option value="null"> Escolha um Ap ou todos</option>
                                <option value="todos"> Todas as zonas </option>
                                @foreach(var i in Model.Zones)
                                {
                                    <option [email protected]_id> @i.zone_name </option>
                                }
                            </select>
                        </div
                    </div>  
                }
                        
                <div class="form-group"style="margin-bottom:2%;margin-top:2%;">
                    <input type="submit" value="Consultar" class="btn btn-primary" />
                </div>
            </div>
            <center>
        </form>

And then to the view Index(that arrives as null)

There is the code of my Controllers.

public IActionResult Filtros1()
        {
            dadosPassar ds = new dadosPassar();
            return View(ds);
        }

        public IActionResult Filtros(dadosPassar ds)
        {
            Console.WriteLine("Filtros");
            dadosPassar dp = new dadosPassar();
            dp.ApZona = ds.ApZona;
            if (ds.ApZona == "null")
            {
                return View("Filtros1");
            }
            else 
                if(ds.ApZona == "aps")
                {
                    
                    var objAPs = _db.L_AccessPoint.ToList();
                    dp.Aps = objAPs;
                    return View(dp);
                }
                else
                {
                    var objZones = _db.L_Zone.ToList();
                    dp.Zones = objZones;
                    return View(dp);
                }

Thats where i verify the values but the ds.ApZona arrives as null.

public IActionResult Index(dadosPassar ds)
        {
            var model = new dadosPassar();

            if (ds.Inicial >=ds.Final || ds.AcessoVisita == "null" || ds.tempo == "null" || ds.ap== "null" || ds.ApZona == "null")
            {
                
                var objAPs = _db.L_AccessPoint.ToList();
                model.Aps = objAPs;
                model.ApZona = ds.ApZona;
                return View("Filtros", model);
            }


So how can i do to pass the variable value to the view Index. How can I pass the value to the view Index? Can you help me passing the variable value into the view Index?



Solution 1:[1]

Please add the[HttpPost] Attribute on the target method as follow:

[Httppost]
    public IActionResult Filtros(dadosPassar ds)
    {
    }

in your view:

<form asp-action="Index" method="post">
......
<form>

in your controller:

public IActionResult Index()
    {
    }
[httpPost]
public IActionResult Index(dadosPassar ds)
    {
    }

If you don't add [httpPost] on your action,it will be a httpget method in default

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1