Am new to ASP.NET MVC and have been looking to build out the standard ASP.NET MVC template with Identity 2.0 installed from Nuget
I have a College class declared as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Placementv2.Models
{
public class College
{
public int CollegeID { get; set; }
public string Name { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public virtual County County { get; set; }
public int CountyID { get; set; }
public string MobilePhone { get; set; }
public string ContactName { get; set; }
public string ContactMobilePhone { get; set; }
public bool CollegeStatus { get; set; }
public virtual ICollection<Course> Courses { get; set; }
public double Latitude { get; set; }
public double Longtitude { get; set; }
public DateTime LastModified { get; set; }
public string UserLastModified { get; set; }
}
}
I also have a Course class as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
namespace Placementv2.Models
{
public class Course
{
[Key]
public int CourseID { get; set; }
public string CourseName { get; set; }
public int CollegeID { get; set; }
public bool CourseStatus { get; set; }
public virtual College College { get; set; }
}
}
I am then trying to follow the Contoso University ASP.NET MVC5 sample project (see https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application )to achieve a similar approach taken with instructors and courses where on selecting a college you can drill in to see the specific courses (and their attributes) within the College index view
I then adapted the College Controller
adapting the simple
public ActionResult Index()
{
return View(db.Colleges.ToList());
}
and replacing this with
public ActionResult Index(int? id, int? courseID)
{
var viewModel = new CollegeIndexData();
viewModel.Colleges= db.Colleges
.Include(i => i.Address1)
.Include(i => i.Address2)
.Include(i => i.Address3)
.Include(i => i.County.CountyName)
.Include(i => i.CollegeStatus)
.Include(i => i.ContactMobilePhone)
.Include(i => i.ContactName)
.Include(i => i.Name)
.Include(i => i.Courses.Select(c => c.CourseID))
.OrderBy(i => i.Name);
if (id != null)
{
ViewBag.CollegeID = id.Value;
viewModel.Courses = viewModel.Colleges.Where(
i => i.CollegeID == id.Value).Single().Courses;
}
return View(viewModel);
}
Finally, I have updated the Index view of College as follows:
@model Placementv2.ViewModels.CollegeIndexData
@{
ViewBag.Title = "Colleges";
}
<h2>Instructors</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>Name</th>
<th>Address1</th>
<th>Address2</th>
<th>Address3/th>
<th>County/th>
<th>Courses</th>
<th></th>
</tr>
@foreach (var item in Model.Colleges)
{
string selectedRow = "";
if (item.CollegeID == ViewBag.CollegeID)
{
selectedRow = "success";
}
<tr class="@selectedRow">
<td>
@Html.DisplayFor(modelItem => item.Address1)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address2)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address3)
</td>
<td>
@Html.DisplayFor(modelItem => item.County.CountyName)
</td>
<td>
@{
foreach (var course in item.Courses)
{
@course.CourseID @: @course.CourseName<br />
}
}
</td>
<td>
@Html.ActionLink("Select", "Index", new { id = item.CollegeID}) |
@Html.ActionLink("Edit", "Edit", new { id = item.CollegeID }) |
@Html.ActionLink("Details", "Details", new { id = item.CollegeID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.CollegeID })
</td>
</tr>
}
</table>
@if (Model.Courses != null)
{
<h3>Courses Offered by Selected College</h3>
<table class="table">
<tr>
<th></th>
<th>Name</th>
<th>Status</th>
</tr>
@foreach (var item in Model.Courses)
{
string selectedRow = "";
if (item.CourseID == ViewBag.CourseID)
{
selectedRow = "success";
}
<tr class="@selectedRow">
<td>
@Html.ActionLink("Select", "Index", new { courseID = item.CourseID })
</td>
<td>
@item.CourseName
</td>
<td>
@item.CourseStatus
</td>
</tr>
}
</table>
}
}
However it throws an exception:
A specified Include path is not valid. The EntityType 'IdentitySample.Models.College' does not declare a navigation property with the name 'Address1'.
INterestingly (and a little frustratingly, as I am totally confused here!) the College Model does not live in the IdentitySamples namespace but in the Placementv2 namespace.
Could someone help me out here in pointing me in the right direction and also in highlighting suggestions on any other extant code errors in the code samples
Thank you in advance
The issue is here:
viewModel.Colleges= db.Colleges
.Include(i => i.Address1)
.Include(i => i.Address2)
.Include(i => i.Address3)
.Include(i => i.County.CountyName)
.Include(i => i.CollegeStatus)
.Include(i => i.ContactMobilePhone)
.Include(i => i.ContactName)
.Include(i => i.Name)
.Include(i => i.Courses.Select(c => c.CourseID))
.OrderBy(i => i.Name);
.Include is for related tables (Navigation properties) here all these fields you are trying to include are table properties.
Change it to:
viewModel.Colleges = db.Colleges.ToList();
or if you want it ordered by Name
then:
viewModel.Colleges = db.Colleges.OrderBy(c => c.Name).ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.