HOW TO USE A FOREACH IN A VIEW PAGE

jayprakash sharma

Total Post:117

Points:821
Posted by  jayprakash sharma
C# 
C#
 1111  View(s)
Ratings:
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

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: how to use a foreach in a view page

    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>

        }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!