如何使用视图模型在表中显示数据

时间:2015-10-06 12:06:26

标签: asp.net-mvc asp.net-mvc-5

我是MVC新手,希望以表格形式在视图中显示两个模型的数据。 这是我的客户模型:

 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; }
        }
    }

这是我的雇佣模式:

 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; }
    }

这是我的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; }


        }

这是我用来将视图模型传递给视图的控制器:

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

这是我的观点:

@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>

在我看来,我的@foreachViewModel在当前上下文中不存在

2 个答案:

答案 0 :(得分:1)

有几件事需要检查 -

您已将视图绑定到 -

IEnumerable<FCproject.ViewModel.Custhire>

您提供的Controller代码段返回单个Custhire对象。实际应该返回IEnumerable Custhire的某些内容,例如 -

    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);
    }

其次,你需要更换,

 @foreach (var item in ViewModel.Custhire)

用这个

 @foreach (var item in Model)

因为Model本身包含Custhire的集合。

答案 1 :(得分:0)

创建视图时,请确保其为strongly typed

enter image description here