【问题标题】:View Scaffolding for Second foreign key isn't correct查看第二个外键的脚手架不正确
【发布时间】:2016-06-08 15:10:27
【问题描述】:

我正在使用实体框架版本 6.1.3 在 Visual Studio 中创建一个简单的 3 模型 MVC 5.2.3 示例。

我有一个Vehicle,它与Contract 有一对多关系,Mileage 也与Contract 有一对多关系。

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

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

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

剃刀视图:

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

奇怪的是,当我将“AnnualMileage”的数据类型从 int 更改为 string 下拉列表会根据需要加载“AnnualMileage”属性。有谁知道可以在模型中使用的任何注释来表示应该选择哪个字段进行显示?脚手架生成器正在做出一个有助于控制的决定。

【问题讨论】:

  • 你能附上剃刀视图吗?
  • @Rich 为什么又把它弄坏了?请暂时不要碰它。
  • @DawidFerenczy 认为我们正在同时编辑它。我添加了错误的剃刀视图,所以我在看到你修复它之前替换了它。道歉。
  • 好的,我重新格式化了。将文本格式化为代码块有一个非常简单的规则 - 将其缩进 4 个空格。就是这样。
  • 谢谢,我会记住的。感谢您的编辑。

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


【解决方案1】:

这是因为 Vehicle 模型包含 Name 属性,但 Mileage 不包含,因此 Razor 助手 DropDownList 不知道在 @ 的情况下应该显示什么作为文本987654325@ 并改为显示 ID。

所以你有两种可能:

  • Name 属性添加到Mileage 模型
  • 告诉DropDownList helper 下拉文本使用什么属性

我会选择第二个选项:

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

【讨论】:

  • 在我发布问题后我确实发生了这种情况,因此我将“AnnualMileage”字段更改为“名称”作为测试,删除了控制器和视图并重新生成它们,但问题仍然存在。 RE 你的第二个建议,我正在尝试学习如何生成正确的脚手架,所以还不想放弃它并手动编辑问题
  • 我不认为根据视图的需要改变模型是一个好方法。您应该做相反的事情 - 根据模型更改视图。所以告诉DropDownList 它应该使用什么属性来显示文本。无论如何,它应该命名为Text,而不是Name(但这并不能解释为什么它在Vehicle 的情况下有效)。
  • 我只是将名称更改为测试。我现在正在研究可以应用于属性的属性,以告诉代码生成器在下拉列表中使用它。 [Display] 属性看起来很有希望,所以我接下来会尝试并报告。我重新测试了将属性更改为“文本”,但问题仍然存在。
猜你喜欢
  • 2011-01-02
  • 1970-01-01
  • 2021-09-18
  • 2013-12-30
  • 1970-01-01
  • 2014-11-06
  • 1970-01-01
  • 2015-05-31
  • 1970-01-01
相关资源
最近更新 更多