2

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.

3
  • 2
    You should use viewmodel here to collect data in one place. Commented Jul 20, 2015 at 7:46
  • You can't have two models in one view. You must create a ViewModel class to hold one Person property and one PagedList<Person> property. Like so: public Person Person {get; set;} and public PagedList<Person> People { get; set; } Commented Jul 20, 2015 at 7:50
  • Thansk for the response both. I have tried using a view model but still having issues. Created a viewmodel called PersonViewModel and populated it with what you suggested. I then put the following '@model reference in the @model IEnumerable<DOSTeaDecider.Models.ViewModels.PersonViewModel>' View but still no luck Commented Jul 20, 2015 at 8:25

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Is it good practice to create an instance of the person class at the top of the view like so @{ ViewBag.Title = "Index"; Person person = new Person();} and then do the following to list the properties in the table <th> @Html.DisplayNameFor(model => person.LastName) </th>
Yes, of course, if you want to use the entity many times it will be a good solution to define it on the top.
If it is a solution for your problem, please mark the post as an answer.
0

Just get the display name from the first row:

@Html.DisplayNameFor(model => model.Person[0].FirstName)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.