我正在使用 html.beginform 当我点击一个按钮时我会进入一个不同的动作,但我需要传递我的模型 aspnet c# 中的值

I'm using a html.beginform to when i click in a button i go to a diferente Action, but i need to pass the values that i have inside my model aspnet c#

我正在使用 html.beginform 开发一个学校项目,当我单击一个按钮时,我会转到一个不同的操作,但我需要在视图中传递我的@model 中的值action.How 我可以这样做吗?

@using (Html.BeginForm("ExportToexcel_Click", "L_AccessPoint", FormMethod.Post))
{
    <button type="submit" id="cmdAction" runat="server">
        Export To Excel
    </button>
}

我将在该操作中创建一个文件,为此我需要将视图中的模型传递给操作 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; }
    }

编辑: 我需要将我的 model.dados2 传递给我的视图,以便我可以使用它,我该怎么做?

那是我的 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; }

    }

"I'm using a html.beginform to when I click in a button I go to a diferente Action?"

Yes this is very obvious it should redirect to your controller as per your code. Because <button type="submit" runat="server"> Export To Excel </button> is not correct way to do that. You should try below way.

Correct Way:

 <input type="submit" id="cmdAction" value="Export To Excel"/>

Output:

"But I need to pass the values that I have inside my model aspnet c#"

It seems that you are sending nothing inside your BeginForm submit action as shown below.

@using (Html.BeginForm("ExportToexcel_Click", "L_AccessPoint", FormMethod.Post))
{
    <input type="submit" d="cmdAction" value="Export To Excel"/>
}

Note: Above code will submit null value to your ExportToexcel_Click controller as you are not sending anything inside the form post action.

Demo:

Tough Its not clear what your plan is. But if you would like to load this page with some predefined data in that case, you should initialize the model in your controller Index action then load the value in hidden mode like below:

Controller Action For Intial View:

public IActionResult Index()
        {
            var ds = new dadosPassar();
            ds.AcessoVisita = "Initial AcessoVisita";
            ds.tempo = "Initial Tempo";
            ds.ApZona = "Initial Ap Zona";
            ds.Final = DateTime.Now;
            return View(ds);
        }

View:

@model DotNet6MVCWebApp.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" />
    <input type="submit" d="cmdAction" value="Export To Excel"/>
}

Controller When Submit Button Cliked:

        [HttpPost]
        public ActionResult ExportToexcel_Click(dadosPassar dp)
        {

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

            return RedirectToAction("Index", ds);

        }

Output:

希望这能解决您当前的问题并指导您将值传递给您的控制器 POST 方法。