gpt4 book ai didi

c# - 将复杂数组从 Controller 传递到 View ASP.NET MVC

转载 作者:行者123 更新时间:2023-12-02 13:35:39 25 4
gpt4 key购买 nike

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

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

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

在 ActionResult 的末尾我有:

return View(src);

//Visual Studio tells me that it is a "SearchArrayModel[] src"

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

@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
}

但是当我启动应用程序时出现错误:

The model item passed into the dictionary is of type 'allegrotest.Models.SearchArrayModel[]', but this dictionary requires a model item of type 'allegrotest.Models.SearchArrayModel

我知道这是一个复杂的数组,但我不知道如何将此数组从 Controller 传递到 View 。

我尝试在 View 中写入:

@model allegrotest.Models.SearchArrayModel[]

但是我无法进入@Model内的字段

最佳答案

从假设“填充过程没问题”开始是错误的。

注意:我做出这个假设是因为我发现您对 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"
};

您的 View 正常并且正在工作

@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>
}
}
}

另一个解决方案是使 View 的 model 成为 IEnumerable 并且在 Action 中您可以使 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>
}
}
}
}

关于c# - 将复杂数组从 Controller 传递到 View ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30872677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com