如何从我的MVC5控制器和视图中的查找表返回结果?

时间:2015-02-10 18:34:15

标签: sql-server asp.net-mvc entity-framework ef-database-first

我的架构中有以下表格:

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是Quote实体中的字段,我需要外键到上面提到的表中的LocationTypeID。当我尝试在SQL Server Management Studio中设置这些外键时,我告诉目标表被引用两次。

我需要将LocationTypeDesc值作为我创建的视图的一部分,这是我的报价模型的类型。建议的方法是将上表中的值提供给我的视图,以便用户可以选择描述并将ID存储为报价流程的一部分?我正在使用EF6 Database First和MVC5。

1 个答案:

答案 0 :(得分:1)

首先,当谈到通过EF访问内容时,发布您的实体类,您的表。 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)