Home > DeveloperSection > Forums > how to use a foreach in a view page
jayprakash sharma
jayprakash sharma

Total Post:117

Points:821
Posted on    August-14-2014 5:03 AM

 C# C# 
Ratings:


 1 Reply(s)
 870  View(s)
Rate this:
Trying to pass a list of confirmed orders to the supplier page (checked with breakpoint the list is being past) just having problems using a foreach to display the list in the view.

//SupplierController 

 public ActionResult Index()

 {

 BuyABicycle_Entities db1 = new BuyABicycle_Entities();

 IEnumerable<BicycleOrder> All_Orders = (from c in db1.BicycleOrders

                                                    where c.Id >= 1

                                                    select c).ToList();

  SupplierVM model = new SupplierVM { allOrders = All_Orders };

  return View(model);

}

//SupplierVM

public class SupplierVM

{

  public IEnumerable<BicycleOrder> allOrders { get; set; }

}

Views/Supplier/Index

@model BicycleShop.ViewModels.SupplierVM

    @{

        ViewBag.Title = "Supplier";

        //var orders = (IList<BicycleOrder>) Model.;

        // var orders = (List<BicycleOrder>) Model.Order);

    }

    @using (Html.BeginForm())

    {

    <table>

        @foreach (var _Order in Model.allOrders)

         {

           <text>

               <tr>

                  <td>@_Order.CustomerName</td>      

               </tr>

            </text>

}

    </table>

    <input type="submit" />

}

This throws the error with @foreach (var _Order in Model.allOrders) Compiler Error Message: CS0012: The type 'IdeaBlade.EntityModel.Entity' is defined in an assembly that is not referenced. You must add a reference to assembly 'IdeaBlade.EntityModel, Version=6.1.7.0, Culture=neutral, PublicKeyToken=287b5094865421c0'.

Foreach loop for tables in MVC4 do I need to declare a variable for the list at the top and then run through that

any help appreciated. Thanks



Pravesh Singh

Total Post:411

Points:2881
Posted on    August-14-2014 6:48 AM

Hi Jay,
You can try this:

@foreach (var _Order in Model)

    {

        <text>

        <tr>

            <td>@Html.TextBoxFor(x => x.allOrders)</td>

            <td>@_Order.allOrders</td>

            @<td>@Html.TextBoxFor(x => x.CustomerName, new { @readonly = true }) </td>

            @foreach(var item in _Order)

             {

                 item.ItemProp <br />

             }

        </tr>

        </text>

    }


Don't want to miss updates? Please click the below button!

Follow MindStick