kamyk
kamyk

Reputation: 295

Passing complex array from Controller to View ASP.NET MVC

I have a model in my ASP.NET MVC application:

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

In controller I'm filling it by some data from WebAPI (filling process is okay), I created an array of SearchArrayModel because I have a lot of items to fill from webAPI (It's a webAPI from site similar to ebay), for example some phones with its names, and attributes which u normally see on the auction site).

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

And on the end of the ActionResult I have:

return View(src);

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

I have also View, where I want to get and display the data:

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

But when I'm starting the app I have an error:

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

I know that this is a complex array and I don't know how to pass this array from Controller to View.

I tried to write in View:

@model allegrotest.Models.SearchArrayModel[]

but then I can't get into the fields inside the @Model

Upvotes: 2

Views: 3103

Answers (2)

adricadar
adricadar

Reputation: 10209

Starting from assumption that "filling process is okay" is wrong.

Note: I made this assumption because I see that you are not interested in Model[index] and I noticed in SearchArrayModel[x]; { } the ;.

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"
};

Your View is okay and is working

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

Another solution will be to make the modelof View to be an IEnumerable and in the Action you can make return View(src.ToList());

Also I noticed, when I tested your code, that you have Model.AttribStructTable which is wrong because your Model is a collection and you have to specify which element from model you want to display Model[index] (not posible with IEnumerable), Model.First() or you can iterate through collection.

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

If you iterate through all items from Model will look like this

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

Upvotes: 3

user1023602
user1023602

Reputation:

@model allegrotest.Models.SearchArrayModel[]

which is an array. So you could try

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

or

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

Upvotes: 2

Related Questions