C# ASP.NET MVC 4视图模型

C# ASP.NET MVC 4视图模型,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我是ASP.NET MVC新手,一直在寻找从Nuget安装Identity 2.0的标准ASP.NET MVC模板 我有一个大学班级,声明如下 using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Placementv2.Models { public class College { public int CollegeID {

我是ASP.NET MVC新手,一直在寻找从Nuget安装Identity 2.0的标准ASP.NET MVC模板

我有一个大学班级,声明如下

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; }

    }
}
然后,我尝试遵循Contoso University ASP.NET MVC5示例项目(请参阅),以实现与讲师和课程类似的方法,在选择学院时,您可以钻取学院索引视图中的特定课程(及其属性)

然后我改编了《大学管理员》

适应简单

public ActionResult Index()
        {
            return View(db.Colleges.ToList());
        }
并将其替换为

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);
}
最后,我更新了学院的索引视图,如下所示:

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; }


    }

}
     @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>
}
}
@model Placementv2.ViewModels.CollegeIndexData
@{
ViewBag.Title=“学院”;
}
教官

@ActionLink(“新建”、“创建”)

名称 地址1 地址2 地址3/th> 县/镇> 课程 @foreach(模型中的var项目) { 字符串selectedRow=“”; 如果(item.CollegeID==ViewBag.CollegeID) { selectedRow=“成功”; } @DisplayFor(modelItem=>item.Address1) @DisplayFor(modelItem=>item.Address2) @DisplayFor(modelItem=>item.Address3) @DisplayFor(modelItem=>item.County.CountyName) @{ foreach(项目课程中的var课程) { @course.CourseID@@course.CourseName
} } @ActionLink(“选择”,“索引”,新的{id=item.CollegeID})| @ActionLink(“编辑”,“编辑”,新的{id=item.CollegeID})| @ActionLink(“详细信息”,“详细信息”,新的{id=item.CollegeID})| @ActionLink(“删除”,“删除”,新的{id=item.CollegeID}) } @如果(Model.Courses!=null) { 选定学院提供的课程 名称 地位 @foreach(Model.Courses中的var项) { 字符串selectedRow=“”; 如果(item.CourseID==ViewBag.CourseID) { selectedRow=“成功”; } @ActionLink(“选择”、“索引”,新建{courseID=item.courseID}) @项目.CourseName @项目1.CourseStatus } } }
但是,它会引发一个异常:

指定的包含路径无效。EntityType“IdentitySample.Models.College”未声明名为“Address1”的导航属性

有趣的是(也有点令人沮丧,因为我在这里完全被搞糊涂了!)学院模型并不存在于identitysample名称空间中,而是存在于Placementv2名称空间中

有人能帮我指出正确的方向吗?还有人能帮我强调代码示例中任何其他现存代码错误的建议吗

提前感谢

问题在这里:

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用于相关表(导航属性),此处您尝试包含的所有字段都是表属性

将其更改为:

viewModel.Colleges = db.Colleges.ToList();
或者,如果您想按
Name
排序,则:

viewModel.Colleges = db.Colleges.OrderBy(c => c.Name).ToList();

非常感谢你,太酷了,太棒了。如此简单却又如此难以捉摸!7.