如何 Return 在我的 MVC5 控制器和视图中查找 Table 结果?

How to Return Results from a Lookup Table in My MVC5 Controller and View?

我的架构中有以下 table:

CREATE TABLE [dbo].[LocationType] (
[LocationTypeID]   INT          IDENTITY (1, 1) NOT NULL,
[LocationTypeDesc] VARCHAR (50) NOT NULL,
CONSTRAINT [PK_LocationType] PRIMARY KEY CLUSTERED ([LocationTypeID] ASC)
);

我的引用实体如下:EF生成的数据库第一个实体

using System;
using System.Collections.Generic;

public partial class Quote
{
    public Quote()
    {
        this.QuoteItems = new HashSet<QuoteItem>();
        this.QuotePrices = new HashSet<QuotePrice>();
    }

    public System.Guid QuoteID { get; set; }
    public int Number { get; set; }
    public int TypeID { get; set; }
    public string OrigZip { get; set; }
    public string OrigCity { get; set; }
    public string OrigState { get; set; }
    public Nullable<int> OrigCountryID { get; set; }
    public int OrigLocationType { get; set; }
    public bool OrigLiftGate { get; set; }
    public bool OrigInside { get; set; }
    public string OrigContact { get; set; }
    public string OrigBusiness { get; set; }
    public string OrigAddress { get; set; }
    public string OrigPhone { get; set; }
    public string OrigFax { get; set; }
    public string OrigEmail { get; set; }
    public string DestZip { get; set; }
    public string DestCity { get; set; }
    public string DestState { get; set; }
    public string DestContact { get; set; }
    public string DestBusiness { get; set; }
    public string DestAddress { get; set; }
    public string DestPhone { get; set; }
    public string DestFax { get; set; }
    public string DestEmail { get; set; }
    public int DestLocationType { get; set; }
    public bool DestLiftGate { get; set; }
    public bool DestInside { get; set; }
    public Nullable<int> DestCountryID { get; set; }
    public Nullable<int> DestServicePointID { get; set; }
    public Nullable<int> TrailerTypeID { get; set; }
    public Nullable<int> TrailerPartType { get; set; }
    public Nullable<decimal> AmountOfTrailer { get; set; }
    public Nullable<System.DateTime> ReadyDate { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Company { get; set; }
    public string Phone { get; set; }
    public string PhoneExt { get; set; }
    public string Fax { get; set; }
    public string FaxExt { get; set; }
    public string Email { get; set; }
    public Nullable<double> Mileage { get; set; }
    public Nullable<decimal> Price { get; set; }
    public Nullable<int> Days { get; set; }
    public Nullable<decimal> ShipmentCost { get; set; }
    public Nullable<decimal> CreditCardCharge { get; set; }
    public Nullable<decimal> GrossProfit { get; set; }
    public string Carrier { get; set; }
    public string CarrierContact { get; set; }
    public string CarrierPhone { get; set; }
    public Nullable<System.DateTime> DeliveryDate { get; set; }
    public Nullable<System.DateTime> QuoteDate { get; set; }
    public Nullable<System.Guid> UserID { get; set; }
    public bool CallIn { get; set; }
    public bool ShipDocs { get; set; }
    public string SpecialInstructions { get; set; }
    public string ContactAddress { get; set; }
    public string ContactZip { get; set; }
    public string BillFirstName { get; set; }
    public string BillLastName { get; set; }
    public string BillAddress { get; set; }
    public string BillZip { get; set; }
    public string BillPhone { get; set; }
    public string BillFax { get; set; }
    public string BillEmail { get; set; }
    public Nullable<System.DateTime> ValidPickupDate { get; set; }

    public virtual ICollection<QuoteItem> QuoteItems { get; set; }
    public virtual ICollection<QuotePrice> QuotePrices { get; set; }
}

OrigLocationTypeDestLocationType 是报价实体中的字段,我需要将外键转换为上述 table 中的 LocationTypeID。当我尝试在 SQL Server Management Studio 中设置这些外键时,我被告知目标 table 被引用了两次。

我需要将 LocationTypeDesc 值作为我正在创建的视图的一部分提供,该视图已输入到我的报价模型中。将上述 table 中的值提供给我的视图的建议方法是什么,以便用户可以 select 描述并将 ID 作为报价过程的一部分存储?我将 EF6 数据库优先与 MVC5 结合使用。

首先,在谈论通过 EF 访问内容时,post 您的实体 类, 而不是 您的表。 EF 生成的表大多是无关紧要的。由于我没有你的实体 类 可以使用,我将不得不做出可能正确也可能不正确的假设。

听起来您想要的是在 QuoteLocationType 之间创建一对多关系。您只需要在 Quote 上引用 属性 和(让您的生活更轻松)一个 属性 来跟踪实际的外键列。 EF 将在幕后创建一个隐式列来跟踪外键,但除非它实际上在您的实体上,否则您无法直接访问或存储它。

public class Quote
{
    ...

    [ForeignKey("LocationType")]
    public int LocationTypeID { get; set; }
    public virtual LocationType LocationType { get; set; }
}

您需要 运行 迁移以相应地更新您的表格。此外,ForeignKey 属性在这里并不是严格必需的,但如果没有它,您将依赖 EF 来辨别 int 属性 是引用 属性 的外键。如果遵循命名约定,它通常可以很好地处理这个问题,但我发现直接明确说明它更容易,也更不容易出错。

然后,您只需将 LocationType 选项列表传递给您正在编辑 Quote 的任何视图。最好的方法是通过视图模型,但为了简单起见,我将在这里使用 ViewBag。所以在你的行动中:

ViewBag.LocationTypeChoices = db.LocationTypes.Select(m => new SelectListItem
{
    Value = m.LocationTypeID.ToString(),
    Text = m.LocationTypeDesc
});

最后,在您看来,添加一个下拉列表:

@Html.DropDownListFor(m => m.LocationTypeID, (IEnumerable<SelectListItem>)ViewBag.LocationTypeChoices)