我在Visual Studio中使用Entity框架版本6.1.3創建了一個簡單的3模型MVC 5.2.3示例。查看腳手架的第二個外鍵不正確
我有一個Vehicle
具有1到許多與Contract
和Mileage
關係與Contract
也有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; }
}
奇怪的是當我改變 'AnnualMileage' 的數據類型從int到字符串 下拉被裝入 'AnnualMileage' 屬性按要求。是否有人知道可以在模型中使用哪些註釋來表示應該選擇哪個字段進行顯示?腳手架發電機正在做出決定,這對控制有用。
你可以在調整剃刀視圖? – Balde
@Balde剛剛添加了 – Rich
@Rich你爲什麼再打破它?請不要碰它一會兒。 –