【问题标题】:ASP.NET MVC 4 ViewModelASP.NET MVC 4 视图模型
【发布时间】:2015-08-19 21:10:00
【问题描述】:

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

    }
}

我还有一个 Course 课程如下:

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


    }

}

然后,我尝试遵循 Contoso University ASP.NET MVC5 示例项目(请参阅 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 )以实现与讲师和课程类似的方法,在选择大学时,您可以深入了解具体课程(以及它们的属性)在学院索引视图中

然后我调整了学院控制器

适应简单

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

最后,我更新了 College 的 Index 视图如下:

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

但是它会抛出异常:

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

有趣的是(也有点令人沮丧,因为我在这里完全糊涂了!)College Model 不在 IdentitySamples 命名空间中,而是在 Placementv2 命名空间中。

有人可以帮我指出正确的方向,并突出显示代码示例中任何其他现存代码错误的建议

提前谢谢你

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    问题在这里:

    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();
    

    【讨论】:

    • 非常感谢 CoOl- 效果很好。如此简单却又如此难以捉摸!7
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多