将复杂数组从控制器传递到视图 ASP.NET MVC

本文关键字:视图 ASP NET MVC 复杂 数组 控制器 | 更新日期: 2023-09-27 17:56:28

我的 ASP.NET MVC应用程序中有一个模型:

public class SearchArrayModel
{
    public long ID { get; set; }
    public string Name { get; set; }
    public struct AttribStruct
    {
        public string AttribName { get; set; }
        public string[] AttribValues { get; set; }
    }
    public AttribStruct[] AttribStructTable { get; set; }
}

在控制器中,我通过来自WebAPI的一些数据填充它(填充过程还可以),我创建了一个SearchArrayModel数组,因为我有很多项目要从webAPI填充(这是一个来自类似于ebay的网站的webAPI),例如一些带有其名称的手机,以及您通常在拍卖网站上看到的属性)。

SearchArrayModel[] src = new SearchArrayModel[x];
{
    //filling the fields
}

在行动结果的末尾,我有:

return View(src);
//Visual Studio tells me that it is a "SearchArrayModel[] src"

我还有视图,我想在其中获取和显示数据:

@model allegrotest.Models.SearchArrayModel
@using (Html.BeginForm())
{    
    <h2>@Model.AttribStructTable[1].AttribName</h2>    
    <h3>@Model.AttribStructTable[1].AttribValues[1]</h3> 
    //indexes of arrays are just for testing, if all will be good I will write a loop         
}

但是当我启动应用程序时,我有一个错误:

传递到字典中的模型项的类型为 "寓言。Models.SearchArrayModel[]',但此字典需要 类型为"快板"的模型项目。Models.SearchArrayModel

我知道这是一个复杂的数组,我不知道如何将这个数组从控制器传递到视图。

我试图在视图中写:

@model allegrotest.Models.SearchArrayModel[]

但后来我无法进入@Model内的田野

将复杂数组从控制器传递到视图 ASP.NET MVC

"灌装过程没问题"的假设开始是错误的。

注意:我之所以做出这个假设,是因为我看到你对Model[index]不感兴趣,我注意到SearchArrayModel[x]; { } ;

SearchArrayModel src = new SearchArrayModel
{
    AttribStructTable = new SearchArrayModel.AttribStruct[]
    {
        new SearchArrayModel.AttribStruct{AttribName = "0Nume", AttribValues = new []{"0one", "0two"}},
        new SearchArrayModel.AttribStruct{AttribName = "1Nume", AttribValues = new []{"1one", "1two"}},
    }, 
    Name = "SearchArrayName"
};

您的视图正常且正常工作

@model allegrotest.Models.SearchArrayModel
@using (Html.BeginForm())
{    
     @foreach(var attribStruct in @Model.AttribStructTable)
     {  
         <h2>@attribStruct.AttribName</h2>    
         @foreach(var attribValue in attribStruct.AttribValues)
         {
              <h3>@attribValues</h3> 
         } 
     }       
}

另一种解决方案是使视图模型成为IEnumerable,并在操作中return View(src.ToList());

我还注意到,当我测试您的代码时,您Model.AttribStructTable哪个是错误的,因为您的Model是一个集合,您必须指定要显示模型中的哪个元素Model[index](不适用于IEnumerable),Model.First()或者您可以遍历集合。

@model IEnumerable<allegrotest.Models.SearchArrayModel>
@using (Html.BeginForm())
{   
     @foreach(var attribStruct in @Model.First().AttribStructTable)
     {  
         <h2>@attribStruct.AttribName</h2>    
         @foreach(var attribValue in attribStruct.AttribValues)
         {
              <h3>@attribValues</h3> 
         } 
     }  
}

如果您循环访问Model中的所有项目将如下所示

@model IEnumerable<allegrotest.Models.SearchArrayModel>
@using (Html.BeginForm())
{   
     @foreach(var searchArrayModel in Model)
     {
         @foreach(var attribStruct in @searchArrayModel)
         {  
             <h2>@attribStruct.AttribName</h2>    
             @foreach(var attribValue in attribStruct.AttribValues)
             {
                  <h3>@attribValues</h3> 
             } 
         }
     }
}
@model allegrotest.Models.SearchArrayModel[]

这是一个数组。所以你可以试试

@foreach (SearchArrayModel item in Model)
{
    <h2>@item.AttribStructTable[1].AttribName</h2>    
    <h3>@item.AttribStructTable[1].AttribValues[1]</h3> 
    ..
}

@for (int i = 0; i < Model.Length; ++i)
{
    <h2>@Model[i].AttribStructTable[1].AttribName</h2>    
    <h3>@Model[i].AttribStructTable[1].AttribValues[1]</h3> 
    ..
}