簡體   English   中英

ASP.NET MVC 4 ViewModel

[英]ASP.NET MVC 4 ViewModel

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

    }
}

我也有一個課程課程,如下所示:

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大學ASP.NET MVC5示例項目(請參閱https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-a -asp-net-mvc應用程序的更多復雜數據模型 )以實現與講師和課程相似的方法,在選擇學院時,您可以深入查看特定的課程(及其屬性)在學院索引視圖中

然后,我改編了學院負責人

適應簡單

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命名空間中。

有人可以在這里幫助我指出正確的方向,也可以重點說明代碼示例中其他任何現存代碼錯誤的建議

先感謝您

問題在這里:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM