Ich erstelle ein einfaches 3-Modell MVC 5.2.3 Beispiel in Visual Studio mit Entity-Framework-Version 6.1.3.Anzeigen Gerüst für zweiten Fremdschlüssel ist nicht korrekt
Ich habe ein Vehicle
, die mit Contract
und Mileage
eine 1-Beziehung mit Contract
haben auch eine 1-Beziehung hat.
Contract
hat 2 Fremdschlüssel, einen für Vehicle
und einen für Mileage
. Ich habe einige Datenanmerkungen hinzugefügt, um Schlüssel und Fremdschlüssel anzugeben, die gemäß dem, was ich gelesen habe, unnötig sein sollten, da ich an der erwarteten Namenskonvention festhalte.
In der Contract
Ansicht der erste Fremdschlüssel, der Vehicle
ist, hat eine bevölkerte Dropdown mit den tatsächlichen Fahrzeugen drin, die korrekt ist. Das Problem ist, dass der zweite Fremdschlüssel, der Mileage
, ein Dropdown-Feld mit den IDs Mileage
geladen hat und nicht die tatsächlichen Kilometer.
Jede Hilfe zu diesem würde sehr geschätzt werden. Code unten.
Razor Ansicht:
@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")
}
Ansicht Modelle:
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; }
}
Merkwürdiger, wenn ich den Datentyp 'AnnualMileage' ändern von int nach Zeichenfolge der Dropdown mit dem 'AnnualMileage' Eigenschaft geladen wird nach Bedarf. Kennt jemand eine Anmerkung, die im Modell verwendet werden kann, um auszudrücken, welches Feld für die Anzeige ausgewählt werden soll? Eine Entscheidung wird vom Gerüstgenerator getroffen, der nützlich sein könnte, um die Kontrolle zu haben.
Könnten Sie bei tach die Rasiereransicht? – Balde
@Balde gerade hinzugefügt – Rich
@Rich Warum hast du es wieder gebrochen? Bitte berühren Sie es nicht für eine Weile. –