Reputation: 620
Ia m trying to learn MVC and have been following the Getting Started with Entity Framework 6 Code First using 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) but change it to be a little drinks generator program (basically taking parts out and using my own idea to try and learn).
I have created a personController, Person (Index, Create, Delete, Details & Edit) Views and a person class which is listed below:
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; }
}
}
In my view I would like to display the people based off the properties set in the model so I have something like the following which works great; however in the tutorial I am following they are using PagedList e.g. @model PagedList.IPagedList<ContosoUniversity.Models.Student>
in the view and then they are able to call the properties of PagedList.IPagedList
from the view. The problem I have is if I include two @model
declarations in the view it doesn't like it but if I remove the reference to the person model I can't use the person properties, e.g. model.LastName
and if I remove the reference to the PagedList.IPagedList I am unable to call the properties from this, e.g., 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 }))
I have tried to research and creating references in the person model to the PagedList but so far my efforts have failed. I think I may be missing something simple, but I am learning and that's why I have asked on here so that maybe someone could please advise.
Upvotes: 2
Views: 2726
Reputation: 19
Just get the display name from the first row:
@Html.DisplayNameFor(model => model.Person[0].FirstName)
Upvotes: 0
Reputation: 1673
Using PagedList as a view model, try to display properties of the entity using some explicitly defined object:
var sampleObject = Model.First();
@Html.DisplayNameFor(model => sampleObject.FirstName)
or
@Html.DisplayNameFor(model => new Person().FirstName)
In this case you can use the PagedList in a standard way.
Upvotes: 1