2016-06-08 15 views
0

我在Visual Studio中使用Entity框架版本6.1.3創建了一個簡單的3模型MVC 5.2.3示例。查看腳手架的第二個外鍵不正確

我有一個Vehicle具有1到許多與ContractMileage關係與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' 屬性按要求。是否有人知道可以在模型中使用哪些註釋來表示應該選擇哪個字段進行顯示?腳手架發電機正在做出決定,這對控制有用。

+0

你可以在調整剃刀視圖? – Balde

+0

@Balde剛剛添加了 – Rich

+0

@Rich你爲什麼再打破它?請不要碰它一會兒。 –

回答

1

這是因爲Vehicle模型包含Name財產,但Mileage沒有,所以剃刀幫手DropDownList不知道什麼應該被顯示爲Mileage的情況下,文本顯示的ID來代替。

所以,你有兩種可能性:

  • Name屬性添加到Mileage模型
  • 告訴DropDownList助手使用什麼屬性下拉列表的文本

我會選擇第二個選項:

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

在我發佈問題後,我確實發生了這種情況,所以我將AnnualMileage字段更改爲Name作爲測試,刪除了控制器和視圖並重新生成了它們,但問題依然存在。RE你的第二個建議,我試圖學習如何生成正確的腳手架,所以不想真的放棄並手動編輯問題 – Rich

+0

我不認爲根據視圖的需要改變模型是一個好方法。你應該做相反的事情 - 根據模型改變視圖。所以告訴'DropDownList'它應該爲顯示的文本使用什麼屬性。無論如何,它應該被命名爲「文本」,而不是「名稱」(但這並不能解釋爲什麼它在「Vehicle」的情況下起作用)。 –

+0

我只是將名稱更改爲測試。我現在正在研究可以應用於屬性的屬性的行,以告訴代碼生成器在下拉列表中使用該屬性。 [Display]屬性看起來很有希望,所以我會嘗試下一步並回報。我重新測試了將屬性更改爲「文本」,但問題仍然存在。 – Rich

相關問題