2

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

2 Answers 2

3

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> 
             } 
         }
     }
}
Sign up to request clarification or add additional context in comments.

4 Comments

@karolinka I understand now. You want only the first element or you want to iterate over the Model?
I want to iterate over the model to display ALL the items from my array, I wrote the "1" indexes just for testing
@karolinka I have update my answer. You try to iterate over Model and Model[index].AttribStructTable or just Model.AttribStructTable?
I try to iterate over Model[index]. The 2nd solution from buffjape is working :)
2
@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> 
    ..
}

1 Comment

Your 2nd solution is working. When I tried to write the 1st (with foreach) I can't access the "SearchArrayModel" in my View

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.