2015-07-20 103 views
2

我試圖學習MVC並一直遵循實體框架入門6代碼優先使用MVC 5(https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application),但將其更改爲一個小型飲料生成器程序(基本上將部分代碼和用我自己的想法去嘗試和學習)。在一個MVC視圖中引用多個模型

我已經創建了一個personController,人(索引,創建,刪除,詳細&編輯)意見並在下面

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 


namespace DOSTeaDecider.Models 
{ 
    public class Person 
    { 
     public int ID { get; set; } 
     [DisplayName("First Name")] 
     public string FirstName { get; set; } 
     [DisplayName("Last Name")] 
     public string LastName { get; set; } 
     //public byte[] Photo { get; set; } 
     [DisplayName("Biography")] 
     public string bio { get; set; } 
     [DisplayName("Drink Choice")] 
     public string Drink { get; set; } 
     [DisplayName("Sugar Amount")] 
     public int Sugar { get; set; } 
     [DisplayName("Milk Colour")] 
     public string MilkColour { get; set; } 
     [DisplayName("Milk Dosage")] 
     public string MilkDose { get; set; }  

    } 
} 

列在我看來,一個人的類我想顯示基於斷人在模型中設置的屬性,所以我有類似以下的東西,這很好,在教程中的Howver我正在使用PagedList例如@model PagedList.IPagedList<ContosoUniversity.Models.Student>,然後他們可以從視圖調用PagedList.IPagedList的屬性。我遇到的問題是,如果我在視圖中包含兩個@model聲明,它不喜歡它,但是如果我刪除對人員模型的引用,則不能使用人員屬性,例如model.LastName如果我刪除對PagedList.IPagedList的引用,我無法調用此屬性,例如Model.PageCount

@model IEnumerable<DOSTeaDecider.Models.Person> 
@*@model PagedList.IPagedList<DOSTeaDecider.Models.Person>*@ 
@*@using PagedList.Mvc;*@ 
@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 
@using (Html.BeginForm("Index", "Person", FormMethod.Get)) 
{ 
    <p> 
     Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string) 
     <input type="submit" value="Search" /> 
    </p> 
} 
<table class="table"> 
    <tr> 
     <th> 
      @Html.ActionLink("First Name", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter }) 
      @*@Html.DisplayNameFor(model => model.FirstName)*@ 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.LastName) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Drink) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Sugar) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.MilkColour) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.MilkDose) 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.FirstName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.LastName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Drink) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Sugar) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.MilkColour) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.MilkDose) 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | 
      @Html.ActionLink("Details", "Details", new { id=item.ID }) | 
      @Html.ActionLink("Delete", "Delete", new { id=item.ID }) 
     </td> 
    </tr> 
} 

</table> 
<br /> 
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount 

@Html.PagedListPager(Model, page => Url.Action("Index", 
    new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter })) 

我曾嘗試研究和創造者模型引用到PagedList但到目前爲止,我的努力都失敗了。我想我也許缺少soemthing簡單,但我學習,這就是爲什麼我要求在這裏,所以,也許有人可以請告知

非常感謝 保羅

+2

您應該在這裏使用viewmodel來在一個地方收集數據。 –

+0

在一個視圖中不能有兩個模型。您必須創建一個ViewModel類來保存一個Person屬性和一個PagedList屬性。像這樣:'公共人Person {get;設置;}'和'公共PagedList People {get;組; }' –

+0

Thansk爲迴應。我曾嘗試使用視圖模型,但仍有問題。創建一個名爲PersonViewModel的視圖模型,並用您的建議填充它。然後我在@model IEnumerable '視圖中放置以下'@model引用,但仍然沒有運氣 – Paul

回答

0

使用PagedList作爲視圖模式,嘗試顯示性能實體使用一些明確定義的對象:

var sampleObject = Model.First(); 
@Html.DisplayNameFor(model => sampleObject.FirstName) 

@Html.DisplayNameFor(model => new Person().FirstName) 

在這種情況下,你可以使用PagedList以標準的方式。

+0

在視圖頂部創建person類的實例是一種很好的做法'@ {ViewBag.Title =「Index」; Person person = new Person();}'然後執行以下操作來列出表中的屬性: @ Html.DisplayNameFor(model => person.LastName) ' – Paul

+0

是的,當然,如果你想多次使用該實體將是一個很好的解決方案,將其定義在頂部。 – Kryszal

+0

如果這是您的問題的解決方案,請將帖子標記爲答案。 – Kryszal

相關問題