Roro
Roro

Reputation: 117

How to use a viewmodel to display data in a table

I am new to MVC and would like to display data from two models in a view in table form. this is my Customer model:

 public class Customer


      {
            public int CustomerID { get; set; }

            [Display(Name = "First Name")]

            public string CustomerFirstName { get; set; }

                [Display(Name = "Last Name")]
            public string CustomerLastName { get; set; }

            [Column(TypeName = "VARCHAR")]
            [StringLength(50)]
            [Display(Name = "Email")]
            public string CustomerEmail { get; set; }

            [Column(TypeName = "VARCHAR")]
            [StringLength(50)]
            [Display(Name = "Street")]
            public string CustomerAddress1 { get; set; }

            [Column(TypeName = "VARCHAR")]
            [StringLength(50)]
            [Display(Name = "Suburb")]
            public string CustomerAddress2 { get; set; }

            [Column(TypeName = "VARCHAR")]
            [StringLength(50)]
            [Display(Name = "City")]
            public string CustomerAddress3 { get; set; }

            [Column(TypeName = "VARCHAR")]
            [StringLength(50)]
            [Display(Name = "Postal code")]
            public string PostalCode { get; set; }

            [Column(TypeName = "VARCHAR")]
            [StringLength(10)]
            [Display(Name = "Cellphone Number")]
            public string CustomerCell { get; set; }

            [Display(Name = "Customer Name")]
            public string FullName
            {
                get { return CustomerFirstName + " " + CustomerLastName; }
            }

            public virtual ICollection<Purchase> Purchases { get; set; }
        }
    }

this is my Hire model:

 public class Hire
    {
        public int HireID { get; set; }

         [Display(Name = "Equipment type")]
        public int EquipmentID { get; set; }
        public int PurchaseID { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [Display(Name = "Returned date")]
        public DateTime? ReturnedDate { get; set; }

        public virtual Purchase Purchases { get; set; }
        public virtual Equipment Equipments { get; set; }
    }

This is my ViewModel:

public class Custhire



     {

            public string CustomerFirstName { get; set; }


            public string CustomerLastName { get; set; }


            public string CustomerEmail { get; set; }

            public string CustomerCell { get; set; }

            public int HireID { get; set; }

            public DateTime? ReturnedDate { get; set; }


        }

This is my controller I use to pass the view model to the view:

     public ActionResult Table()
            {
                Custhire model = new Custhire();
                return View(model);
            }

This is my view:

@model IEnumerable<FCproject.ViewModel.Custhire>

@{
    ViewBag.Title = "Table";
}

<h2>Table</h2>

<table class="table table-condensed"
    <thead>
        <tr>
            <th>CustomerID</th>
            <th>Equipment</th>
            <th>Retrned date</th>
            <th></th>
            <th></th>
        </tr>
    </thead>
    <body>
        @foreach (var item in ViewModel.Custhire)
        {
            <tr>
                <td>
                    @Html.DisplayFor(m => item.CustomerFirstName)
                </td>
                <td>
                    @Html.DisplayFor(m => item.CustomerLastName)
                </td>
                <td>
                    @Html.DisplayFor(m => item.CustomerEmail)
                </td>
                <td>
                    @Html.DisplayFor(m => item.CustomerCell)
                </td>
                <td>
                    @Html.DisplayFor(m => item.HireID)
                </td>
                <td>
                    @Html.DisplayFor(m => item.ReturnedDate)
                </td>

            </tr>
        }
    </body>
    <tr>
</table>

in my view, my @foreach says ViewModel does not exist in current context

Upvotes: 1

Views: 3671

Answers (2)

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

When you create view, ensure that is strongly typed

enter image description here

Upvotes: 0

Yogi
Yogi

Reputation: 9739

There are couple of things to check for -

You have bind your view with -

IEnumerable<FCproject.ViewModel.Custhire>

And the Controller snippet that you have provided returns, single Custhire object. Which actually should return IEnumerable of Custhire Something for example like -

    public ActionResult Table()
    {
        List<Custhire> model = new List<Custhire>();

        //keep adding the "Custhire" to the collection
        //as per your requirement
        model.Add(new Custhire());

        return View(model);
    }

Second, you need to replace,

 @foreach (var item in ViewModel.Custhire)

with this

 @foreach (var item in Model)

as the Model itself contains collection of Custhire.

Upvotes: 1

Related Questions