我有一个视图,我将模型定义为:
@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);
}
答案 0 :(得分:1)
为什么模型没有正确传递?
您 无法 在网址查询字符串中传递复杂模型。即使您序列化它,URL也有字符限制。
理想情况下,您只想传递 ID ,然后在plants
操作方法中再次检索ExportExcel
数据。
<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
...
}