[英]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.