0

I have two models below.

        public class CustMaster
        {
            public string  FirstName { get; set; }
            public string  LastName { get; set; }
            public List<DetailsInfo> GetDetails { get; set; }        
        }

        public class DetailsInfo
        {
            public string  OrderId { get; set; }
            public string  OrderDescription { get; set; }
            public string  OrderStatus { get; set; }
            public string  Price { get; set; }        
        }

I use this model in my view to display the result in my html section

        @model IEnumerable<Tester.Domain.GenClasses.CustMaster>

Data is returned in this format

        Row 0
          -FirstName
          -LastName
          -GetDetails
                -Row 0
                  -OrderId
                  -OrderDescription
                  -OrderStatus
                  -Price                      
                -Row 1
                  -OrderId
                  -OrderDescription
                  -OrderStatus
                  -Price
        Row 1
          -FirstName
          -LastName
          -GetDetails
                -Row 0
                  -OrderId
                  -OrderDescription
                  -OrderStatus
                  -Price

                -Row 1
                  -OrderId
                  -OrderDescription
                  -OrderStatus
                  -Price

I want to be able to display data in my html i.e loop through the entire main list and write out the inner data. I am thinking it should be like the code below. Is this correct? What will the parent foreach look like? Will this be a foreach or a for statement? Please assist.

      @foreach(what goes here?)
      {
        @foreach (var m in CustMaster)
        {
            <ul>
            <li>                  
            </li>

            @foreach (var d in m.GetDetails)
            {

               <li>
                    <ul>
                        <li> </li>
                        <li> </li>
                        <li> </li>
                        <li> </li>                                  
                    </ul>
                </li>

              }
            </ul>
        } 
      }
7
  • It returns the model CustMaster populated with data which is fine. Commented Jul 7, 2016 at 14:59
  • As a general note foreach statement works on objects that implements a IEnumerable interface. Then the answer is .. it depends on how the collections of CustMaster objects is defined. Commented Jul 7, 2016 at 15:00
  • Did you see this line. @model IEnumerable<Tester.Domain.GenClasses.CustMaster> Commented Jul 7, 2016 at 15:01
  • Everything is listed in my question. Did you read my question? Thanks. Commented Jul 7, 2016 at 15:02
  • then the answer is .. yes .. you can use foreach. Commented Jul 7, 2016 at 15:03

1 Answer 1

2

You can use foreach(var c in Model)

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I will try this. So this will loop through the list right?
It should loop over your IEnumerable<Tester.Domain.GenClasses.CustMaster>
Thanks. I will try it now.
Actually I got it to work without the outermost foreach statement. It wasn't necessary.

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.