[英]Reference Multiple Models in One MVC View
我正在嘗試學習 MVC,並且一直在遵循使用 MVC 5 的 Entity Framework 6 Code First 入門( 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 )但將其更改為一個小飲料生成器程序(基本上是取出零件並使用我自己的想法嘗試和學習)。
我創建了一個 personController、Person(索引、創建、刪除、詳細信息和編輯)視圖和一個如下所列的 person 類:
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; }
}
}
在我看來,我想根據模型中設置的屬性來顯示人員,所以我有以下類似的東西,效果很好; 但是在我關注的教程中,他們在視圖中使用 PagedList 例如@model PagedList.IPagedList<ContosoUniversity.Models.Student>
然后他們能夠從視圖中調用PagedList.IPagedList
的屬性。 我@model
的問題是,如果我在視圖中包含兩個@model
聲明,它不喜歡它,但是如果我刪除了對 person 模型的引用,我就不能使用 person 屬性,例如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 的引用,但到目前為止我的努力都失敗了。 我想我可能遺漏了一些簡單的東西,但我正在學習,這就是我在這里問的原因,也許有人可以提供建議。
使用 PagedList 作為視圖模型,嘗試使用一些明確定義的對象來顯示實體的屬性:
var sampleObject = Model.First();
@Html.DisplayNameFor(model => sampleObject.FirstName)
或者
@Html.DisplayNameFor(model => new Person().FirstName)
在這種情況下,您可以以標准方式使用 PagedList。
只需從第一行獲取顯示名稱:
@Html.DisplayNameFor(model => model.Person[0].FirstName)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.