【发布时间】: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