我想将我认为的价值观传递给一个行动

I would like to pass the values that i have in my view to a action

我正在使用 html.beginform 开发一个学校项目,当我点击一个按钮时,我会转到一个不同的操作,但我需要传递我在我的@model.dados2 中的值行动的观点,我该怎么做?

<form asp-action="ExportToexcel_Click" 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/>
    <select class="listbox2" hidden asp-for="ap"  multiple>
        @foreach(var i in Model.ap)
        {
            <option value=@i> @i </option>
        }
    </select>

    <div class="form-group"style="margin-bottom:2%;margin-top:2%;">
        <input type="submit" value="Consultar" class="btn btn-primary" />
    </div>
               
</form>

我已经将一些数据传递给新操作,我传递了一个整数列表,但我不知道如何传递一个对象列表。

在该操作中我将创建一个文件,为此我需要将视图中的模型传递给操作 ExportToexcel_Click

[HttpPost]
        public ActionResult ExportToexcel_Click(dadosPassar dp)
        {

            var ds = new dadosPassar();
            ds = dp;

            return RedirectToAction("Index",ds);

        }

这就是我的模型,

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

那是我的 dados2 结构

    public class Stored1
    {
        public short ap_id { get; set; }
        public string ap_name { get; set; }
        public int numeroAcessos { get; set; }
        //public int month { get; set; }
        public int year { get; set; }
        public int MES { get; set; }
        public int DIA { get; set; }

    }

"但我不知道如何传递对象列表"

You could follow below steps:

加载视图的控制器操作:

public IActionResult Index(string stored1s)
        {
            if (stored1s != null)
            {
                List<Stored1> model = JsonConvert.DeserializeObject<List<Stored1>>(stored1s);
            }

            var listDados = new List<Stored1>()
            {
                new Stored1(){ ap_id = 101,ap_name ="AP-101", year= 2021, DIA=101, MES=202},
                new Stored1(){ ap_id = 102,ap_name ="AP-102", year= 2022, DIA=102, MES=203},
                new Stored1(){ ap_id = 103,ap_name ="AP-103", year= 2023, DIA=103, MES=204},


            };
            var ds = new dadosPassar();
            ds.AcessoVisita = "Initial AcessoVisita";
            ds.tempo = "Initial Tempo";
            ds.ApZona = "Initial Ap Zona";
            ds.Final = DateTime.Now;
            ds.dados2 = listDados;
            return View(ds);
        }

Note: Here string stored1s will be assigned value when RedirectToAction will be called from ExportToexcel_Click method.

替代索引

   public IActionResult Index()
        {
            var stringList = TempData["dados2"].ToString();
            List<Stored1> datosList = JsonConvert.DeserializeObject<List<Stored1>>(stringList);

            var listDados = new List<Stored1>()
            {
                new Stored1(){ ap_id = 101,ap_name ="AP-101", year= 2021, DIA=101, MES=202},
                new Stored1(){ ap_id = 102,ap_name ="AP-102", year= 2022, DIA=102, MES=203},
                new Stored1(){ ap_id = 103,ap_name ="AP-103", year= 2023, DIA=103, MES=204},


            };
            var ds = new dadosPassar();
            ds.AcessoVisita = "Initial AcessoVisita";
            ds.tempo = "Initial Tempo";
            ds.ApZona = "Initial Ap Zona";
            ds.Final = DateTime.Now;
            ds.dados2 = listDados;
            return View(ds);
        }

查看:

@model DotNetWebApp.Models.dadosPassar

@using (Html.BeginForm("ExportToexcel_Click", "L_AccessPoint", FormMethod.Post))
{
    <input asp-for="AcessoVisita" hidden class="form-control" />
    <input asp-for="tempo" hidden class="form-control" />
    <input asp-for="ApZona" hidden class="form-control" />

    @for (int i = 0; i < Model.dados2.Count; i++)
    {
        
        <tr >
            <td>
                <label><strong>Id</strong></label>
                @Html.TextBoxFor(model => model.dados2[i].ap_id)
            </td>
            <td>
                <label><strong>Name</strong></label>
                @Html.TextBoxFor(model => model.dados2[i].ap_name)
            </td>
            <td>
                <label><strong>Year</strong></label>
                @Html.EditorFor(model => model.dados2[i].year)
            </td>

        </tr>
        <br />
    }
    <input type="submit" d="cmdAction" style="margin-top:10px" class="btn btn btn-primary" value="Export To Excel" />
}

Note: You can hide your @Html.TextBoxFor if you don't want them to display. Up to you.

控制器提交时:

        [HttpPost]
        public ActionResult ExportToexcel_Click(dadosPassar dp)
        {

            var ds = new dadosPassar();
            ds = dp;
            return RedirectToAction("Index",
    new
    {
        stored1s = JsonConvert.SerializeObject(dp.dados2)
    });

           

        }

If you prefer Alternative Index in that case use this ExportToexcel_Click

        [HttpPost]
        public ActionResult ExportToexcel_Click(dadosPassar dp)
        {

            var ds = new dadosPassar();
            ds = dp;
            TempData["dados2"] = JsonConvert.SerializeObject(dp.dados2);
            return RedirectToAction("Index");
    



        }

输出: