Ich habe ein Problem, bei dem ich denke, dass ich die Dinge richtig eingerichtet habe, aber die Ergebnisse sind immer gleich Null.Wie verwende ich zwei Dropdown-Menü-Instanzen aus demselben Modell?
hier die drei Modelle:
public class FamilyMember
{
[Key]
public int id { get; set; }
[Required]
[Display(Name = "First Name")]
public string firstName { get; set; }
[Required]
[Display(Name = "Surname")]
public string surname { get; set; }
[Required]
[Display(Name = "Date of Birth")]
public DateTime dob { get; set; }
public virtual FamilyRelationship FamilyRelationship { get; set; }
[Display(Name = "Full Name")]
public string fullName
{
get
{
return string.Format("{0} {1}", firstName, surname);
}
}
}
public class RelationshipType
{
[Key]
public int id { get; set; }
[Required]
[Display(Name="Relationship Type")]
public string relationshipType { get; set; }
public virtual FamilyRelationship FamilyRelationship { get; set; }
}
public class FamilyRelationship
{
[Key]
public int id { get; set; }
[ForeignKey("FamilyMembers")]
[Display(Name = "First Family Member")]
public int familyMemberPrimary { get; set; }
[ForeignKey("FamilyMembers")]
[Display(Name = "Second Family Member")]
public int familyMemberSecondary { get; set; }
[Display(Name = "Relationship Type")]
public int relationshipType { get; set; }
public virtual ICollection<FamilyMember> FamilyMembers { get; set; }
public virtual ICollection<RelationshipType> RelationshipTypes { get; set; }
}
So habe ich erfolgreich Daten zu FamilyMember
hinzugefügt und RelationshipType
und die CRUD perfekt funktioniert.
Das Problem ist in der Create Controller/View von FamilyRelationship
gefunden. Das Dropdown-Menü funktioniert einwandfrei und zeigt die Familienmitglieder in den beiden zugehörigen Menüs an. Die Beziehung wird auch im Dropdown-Menü relationType
angezeigt. Wenn ich jedoch auf create klicke, werden alle Werte auf null gesetzt.
-Controller erstellen:
// GET: FamilyRelationships/Create
public ActionResult Create()
{
ViewBag.familyMember = new SelectList(db.FamilyMembers, "id", "fullName");
ViewBag.relationship = new SelectList(db.RelationshipTypes, "id", "relationshipType");
return View();
}
// POST: FamilyRelationships/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "id,familyMemberPrimary,familyMemberSecondary,relationshipType")] FamilyRelationship familyRelationship)
{
if (ModelState.IsValid)
{
db.FamilyRelationships.Add(familyRelationship);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(familyRelationship);
}
Ansicht erstellen:
@model FamilyTree.Models.FamilyRelationship
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>FamilyRelationship</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.familyMemberPrimary, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.familyMemberPrimary, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.familyMemberPrimary, "", new { @class = "text-danger" })*@
@Html.DropDownList("familyMember", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.familyMemberPrimary, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.familyMemberSecondary, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("familyMember", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.familyMemberPrimary, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.relationshipType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("relationship", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.relationshipType, "", 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")
}
Bitte lassen Sie mich wissen, wo soll ich nur falsch und wenn möglich ein Beispiel geben, um diese Arbeit zu machen.
Das ist fantastisch. Schätzen Sie Ihre Bemühungen, damit zu helfen. –