通过按钮 MVC Net Core 将 IEnumerable 模型传递给控制器

Pass an IEnumerable model to a controller via button MVC Net Core

我有一个视图,我将模型定义为:

@model IEnumerable <OnePortal.Models.Plant>

在这个视图中我有一个按钮,它应该根据以下内容将模型传递给控制器​​:

<button class="btn btn-info" type="button" onclick="location.href='@Url.Action("ExportExcel", "Customer", new {gata = Model}, null)'">

如您所见,动作称为 "ExportExcel",控制器称为 "Customer"。

Customer 控制器中的操作如下所示:

public IActionResult ExportExcel(IEnumerable<Plant> gata){ 
}

但是,当我调试控制器时,它说传递的参数 (gata) 有 count=0,即为空。我知道模型不为空,因为我稍后在视图中循环遍历模型并显示其内容

有人可以帮我解决这个问题吗?为什么模型没有正常通过?

编辑

    [HttpGet]
    public IActionResult Device(string id)
    {
        R2S.ServiceClient r2s = new R2S.ServiceClient();
        AnlData[] adData = r2s.GetAnlDataListAsync(new string[] { "STATION" }, new string[] { id }).Result; // lista med anläggningsdata 
        Apparat[] appData = r2s.GetApparaterAsync(new string[] {"STATION" }, new string[] { id }).Result; // lista med apparatdata

        var plants = PopulatePlantList(adData);//Skapar en lista över alla anläggningar
        var devices = PopulateDeviceList(appData);//Skapar en lista över alla apparater

        var viewModel = new PlantDeviceViewModel
        {
            Plants = plants,
            Devices = devices
        };

        return View(viewModel);
    }

Why is the model not passed properly?

不能在URL查询字符串中传递复杂模型。即使你序列化它,URL 也有字符限制。

理想情况下,您只想传递 Id,然后在 ExportExcel 操作方法中再次检索 plants 数据.

<button class="btn btn-info" type="button" 
    onclick="location.href='@Url.Action("ExportExcel", "Customer", 
    new {id = ViewData["Id"]}, null)'">

[HttpGet]
public IActionResult Device(string id)
{
   ...
   var viewModel = new PlantDeviceViewModel
   {
      Plants = plants,
      Devices = devices
   };
   ViewData["Id"] = id;
   return View(viewModel);
}

[HttpGet]
public IActionResult ExportExcel(string id)
{
   R2S.ServiceClient r2s = new R2S.ServiceClient();
   Apparat[] appData = r2s.GetApparaterAsync(
       new string[] {"STATION" }, new string[] { id }).Result; // lista med apparatdata

   var plants = PopulatePlantList(adData);//Skapar en lista över alla 
   ...
}