Error when trying to list a model inside a model ASP.NET MVC

I am trying to list a model within a model in order to perform a kind of join in the database.
I have two models:

public partial class Orders
{
    [System.ComponentModel.DataAnnotations.DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public Nullable<System.DateTime> Data_Registo { get; set; }
    public string Num_Encomenda { get; set; }
    public string Ref_Cliente { get; set; }
    public string Colecao { get; set; }
 }

and

public partial class Colors
{
    public int ID_Programa { get; set; }
    public int ID_Linha_Cor { get; set; }
    public string Color { get; set; }
}

I am trying to present these two models in my view because I want my order listing to have a table to load in colors associated with my order.
For that I created a new model with the previous models

public class OrderViewModel
{
    public IEnumerable<Orders> order{ get; set; }
    public IEnumerable<Colors> color{ get; set; }
}

and my controller:

 public ActionResult Index()
    {
        var orders= from m in db.Orders
                        select m;
        var cores = from c in db.Colors
                        select c;
        var tables = new OrderViewModel
        {
            order = db.Orders.ToList(),
            color = db.Colors.ToList(),
        };
        return View(orders.ToList());

in my view i’m trying to create a table with the orders from inside another table listing the associated colors:

@model Balu3._0.ViewModel.OrderViewModel
<table class="table table-borderless table-sm " ;>
@foreach (var Order in Model.order)
{
    <tr style="border-top: 2px solid #cdd0d4;">
        <td style="width: 130px;">
            <b>Artigo: </b>@Order.Cod_Artigo
        </td>
        <td colspan="8">
            <b>Modelo: </b>@Order.Modelo
        </td>
    </tr>
  }
  <tr>
        <td colspan="8">
            <table class="table table-bordered">
                @foreach (var Color in Model.color)
                {                 
                    <tr>
                        <td style="width: 150px;">
                            @Color.Color)
                        </td>
                        <td style="width: 150px;">
                        </td>
                        <td style="width: 150px;">
                            <!--Talão-->
                        </td>
                        <td colspan="2">
                            @Html.DropDownList("startServ", "All")
                        </td>
                        <td colspan="5">
                            <textarea name="message" rows="1" cols="60"></textarea>
                        </td>
                    </tr>
                 }
            </table>
        </td>
    </tr>

and when I run my application I have the following error:

The model item passed into the dictionary is of type
‘System.Linq.OrderedEnumerable2 [Balu3._0.Models.Program, System.Nullable1 [System.DateTime]]’ , but this dictionary requires a
model item of type ‘Balu3._0.ViewModel.EncomendaViewModel’.

Note: I checked all syntax errors and I believe the problem has nothing to do with it. The code may have syntax errors since I changed some names to better understand my code

Update:for now i just want to list the colors without being associated with the order.

Thanks for the help you can give me!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Change you action to this

public ActionResult Index()
    {
               
        var tables = new OrderViewModel
        {
            order = db.Orders.ToList();
            color = db.Colors.ToList();
        };

return View(tables);

````


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x