2017-06-21 46 views
0

我其中我已经定义了模型的图:传递一个IEnumerable模型到控制器

@model IEnumerable <OnePortal.Models.Plant> 

在这个视图我有一个按钮,这应该模型传递给一个控制器根据:

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

正如你所看到的,动作被称为“ExportExcel”和控制器“客户”。

客户控制器内的动作是这样的:

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

然而,当我debugg控制器,它说,通过(GATA)该参数具有计数= 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); 
    } 
+0

'gata'将会是'Model'的字符串表示形式,如果您检查到的不是您想要的任何地方。你需要找出一种方法将你的模型序列化成适合url的东西。作为一个方面说明,如果你有一个按钮做一个锚点的工作,那么只需使用一个锚点 – Shoe

+0

你可以显示第一个视图的操作方法吗?我想看看你如何得到'IEnumerable '。 – Win

+0

你好@惠。我刚刚编辑了我的帖子。希望这回答你的问题。 – jazy

回答

1

为什么模型不正确地传递?

无法传递URL查询字符串的复杂模型。即使您将其序列化,URL也具有字符限制。

理想情况下,你只是想通过标识,那里面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 
    ... 
} 
+0

这工作!非常感谢你@Win! – jazy

相关问题