Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8502399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:28:26+00:00 2026-06-11T01:28:26+00:00

I would like to retrieve several items in one cell of a table in

  • 0

I would like to retrieve several items in one cell of a table in my view. I have several models, Product, Pack(as category), Order and OrderDetail. I think to achieve what I want, I should create a view model with data from my Models, but I don’t really know how to do this and wich data i should bind.

This my view

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.OrderId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Username)          
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ClientID)          
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SiteNumber)
        </td>   
        <td>
            @if (item.Pack == null)
            {
                    <ul>
                        @foreach (var tmp in item.????) //PRODUCT
                        { 
                           <li>Html.DisplayFor(modelItem => tmp.Name) </li>
                        } 
                    </ul>
            }
            else
            {                 
                    <ul>
                        @foreach (var tmp in item.????) //PACK
                        { 
                            <li>Html.DisplayFor(modelItem => tmp.Name) </li> 
                        } 
                    </ul>                       
            }
        </td>

        <td>
        @{
                if (item.Pack == null)
                {
                        <ul>
                            @foreach (var tmp in item.????)  // PRODUCT
                            { 
                                <li>Html.DisplayFor(modelItem => tmp.UnitPrice)
                               </li>
                            }
                        </ul>
                }
                else
                {                    
                        <ul>
                                @foreach (var tmp in item.????) //PACK
                                { 
                                    <li>
                                    @Html.DisplayFor(modelItem => tmp.UnitPricePack)
                                    </li>
                                }
                        </ul>
                }
}

        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Quantity)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Total)
        </td>        
        <td>
            @Html.DisplayFor(modelItem => item.OrderDate)
        </td>
    </tr>  

I would like my table looks like that at the end :

enter image description here

My question again, How can I create a ViewModel to retrieves data in my view and for it match with my @foreach condition inside ma table… I don’t know if I am clear enough… This post follow this one . Actually, what i’ve done returns me error on error, I don’t really know how to set up my viewmodel, which data I need to bind and how. Thanks for your help, your advices, I take all

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:28:28+00:00Added an answer on June 11, 2026 at 1:28 am

    You don’t necessarily need a new ViewModel, Order has all you need. Your model will be a list of orders.

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.OrderId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Username)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ClientID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SiteNumber)
            </td>
            <td>
                <ul>
                    @foreach (var o in item.OrderDetails)
                    {
    
                        if (o.Pack == null)
                        {
                        <li>@Html.DisplayFor(modelItem => o.Product.Name) </li> //PRODUCT
                        }
                        else
                        {
    
                        <li>@Html.DisplayFor(modelItem => o.Pack.Name) </li> // PACK
    
                        }
                    }
                </ul>
            </td>
            <td>
                    <ul>
                        @foreach (var o in item.OrderDetails)
                        {
    
                            if (o.Pack == null)
                            {
                            <li>@Html.DisplayFor(modelItem => o.UnitPrice) </li> //PRODUCT
                            }
                            else
                            {
                            <li>@Html.DisplayFor(modelItem => o.UnitPricePack) </li>  //PACK
                            }
                        }
                    </ul>
            </td>
            <td>
                <ul>
                    @foreach (var o in item.OrderDetails)
                    {
                        <li>@Html.DisplayFor(modelItem => o.Quantity)</li>
                    }
                </ul>
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Total)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.OrderDate)
            </td>
        </tr>  
    }
    

    Update according to your update:

    You are still trying to pass collection of orderdetails instead of orders

    instead of this:

            var user = from m in db.OrderDetails
                       select m;
    

    Have this ( assuming you have Orders table):

        public ActionResult Index(string searchSite, string searchString)
        {
    
            var user = from m in db.Orders
                       select m;
    
            if (!String.IsNullOrEmpty(searchString))
            {
                user = user.Where(s => s.ClientID.Contains(searchString));                
            }
    
            if (!String.IsNullOrEmpty(searchSite))
            {
                user = user.Where(c => c.SiteNumber.Contains(searchSite));
            }
                return View(user);
        }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to retrieve an ordered query result, but I need to have
I would like to retrieve the categories of my website in order to build
I have a set of URLs for which I would like to retrieve the
I have an SQL Server 2000 db, and I would like to retrieve summary
I have several navigation related functions which I would like to have no depth
I have a layout that contains several sub-views all which set height like: <View
I would like to retrieve the AppSetting key from the assembly config file called:
I would like to retrieve the contents of a file, filter and modify them
I am saving images to the photo library and would like to retrieve them
I'm using the python suds module and would like to retrieve the response headers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.