查看第二个外键的脚手架是否正确

时间:2016-06-08 15:10:28

标签: c# asp.net-mvc entity-framework

我在Visual Studio中使用Entity framework version 6.1.3创建一个简单的3模型MVC 5.2.3示例。

我的VehicleContract有1对多的关系,MileageContract的关系也有1对多。

Contract有2个外键,一个用于Vehicle,另一个用于Mileage。我已经添加了一些数据注释来指定键和外键,根据我读过的内容应该是不必要的,因为我已经坚持预期的命名约定。

Contract视图中,Vehicle的第一个外键具有填充的下拉列表,其中包含实际车辆,这是正确的。问题是第二个外键Mileage的下拉列表中加载了Mileage ID,而不是实际的里程数。

对此的任何帮助将不胜感激。代码如下。

Razor观点:

@model RRSMVCTest5.Models.Contract

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Contract</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.VehicleId, "VehicleId", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("VehicleId", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.VehicleId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.MileageId, "MileageId", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("MileageId", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.MileageId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.InitialPayment, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.InitialPayment, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.InitialPayment, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.MonthlyPayment, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.MonthlyPayment, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.MonthlyPayment, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

查看模型:

public class Contract
{
    //[Key]
    public int Id { get; set; }        
    public int VehicleId { get; set; }

    [ForeignKey("VehicleId")]
    virtual public Vehicle Vehicle { get; set; }        
    public int MileageId { get; set; }

    [ForeignKey("MileageId")]
    virtual public Mileage Mileage { get; set;}

    public double InitialPayment { get; set; }
    public double MonthlyPayment { get; set; }
}

public class Vehicle
{
    public Vehicle()
    {
        Contracts = new List<Contract>();
    }
    [Key]
    public int VehicleId { get; set; }
    public string  Name { get; set; }
    public virtual ICollection<Contract> Contracts { get; set; }               
}

public class Mileage
{
    public Mileage()
    {
        Contracts=new List<Contract>();
    }
    [Key]
    public int MileageId { get; set; }
    public int AnnualMileage { get; set; }
    public virtual ICollection<Contract> Contracts { get; set; }
}

奇怪的是,当我更改&#39; AnnualMileage&#39;的数据类型时从int到string 下拉列表会加载“年度里程”&#39;财产根据要求。有没有人知道可以在模型中使用的任何注释来表示应该选择哪个字段进行显示?脚手架生成器正在做出决定,可以控制。

1 个答案:

答案 0 :(得分:1)

那是因为Vehicle模型包含Name属性,但Mileage没有,所以Razor助手DropDownList没有&#39} ; t知道在Mileage的情况下应该显示为文本的内容,而是显示ID。

所以你有两种可能性:

  • Name属性添加到Mileage模型
  • 告诉DropDownList帮助者用于下拉文字
  • 的属性

我会选择第二个选项:

@Html.DropDownList("DropDownId", Model.Select(item => new SelectListItem
{
    Value = item.MileageId.ToString(),
    Text = item.AnnualMileage.ToString()
}))