我的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填充的项目(它来自网站的webAPI)类似于ebay),例如一些带有名字的手机,以及你通常在拍卖网站上看到的属性。)
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
}
但是当我启动应用时,我遇到了错误:
传递到字典中的模型项是类型的 &#39; allegrotest.Models.SearchArrayModel []&#39;,但这本字典需要 类型&#39; allegrotest.Models.SearchArrayModel
的模型项
我知道这是一个复杂的数组,我不知道如何将此数组从Controller传递给View。
我试着在View中写道:
@model allegrotest.Models.SearchArrayModel[]
然后我无法进入@Model
内的字段答案 0 :(得分:3)
假设 &#34;填充过程没问题&#34; 是错误的。
注意:我做了这个假设,因为我发现您对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>
}
}
}
}
答案 1 :(得分:2)
@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>
..
}