2017-01-16 3 views
0

Ich bin ziemlich neu in MVC und hatte mehrere Lösungen gesucht und überprüft, aber keine für mich noch gearbeitet.asp.net MVC - Code ersten Fremdschlüssel lässt mich nichts eingeben

Ich habe eine Datenbank mit Code First erstellt. Es hat 3 Tische: Club, Match und Round. In jeder Runde gibt es ein paar Matches. Spiele in jedem Spiel 2 Schläger. Match enthält einen Fremdschlüssel für Round (der hervorragend funktioniert) und zwei separate Mehrfachschlüssel für Match (Heim- und Auswärtsmannschaft).

Wenn ich MatchesController mit Ansichten mit EF hinzufügen und starten Sie die Anwendung und versuchen, neue Übereinstimmung einzugeben, habe ich eine Dropdown-Liste von Runden aus der Datenbank - funktioniert erstaunlich. Ich hätte gerne zwei Dropdown-Listen mit Teamnamen/IDs aus der Datenbank, so dass ich Teams auswählen kann, die spielen, aber aus irgendeinem Grund habe ich zwei Textfelder, mit denen ich alles schreiben kann, als ob die FK-Beschränkungen nicht existieren.

Ich habe ein Problem damit, weil ich nicht sicher bin, was ich falsch mache - sind es die Fremdschlüssel? Ich habe sie so codiert, während ich andere Fragen durchblätterte, aber vielleicht habe ich es vermasselt?

Runde Klasse:

public class Round : IValidatableObject 
{ 

    public int RoundID { get; set; } 

    [Required] 
    public int RoundNumber { get; set; } 

    public virtual ICollection<Match> Matches { get; set; } 
} 

Clubklasse:

public class Club 
{ 
    public int ClubID { get; set; } 

    [Required] 
    public string ClubName { get; set; } 

    public virtual ICollection<Match> HomePlays { get; set; } 
    public virtual ICollection<Match> AwayPlays { get; set; } 
} 

Spiel-Klasse und ihre DB Kontextklasse:

public class Match 
{ 

    public int MatchId { get; set; } 

    public int HomeClubId { get; set; } 
    public int AwayClubID { get; set; } 

    public virtual Club HomeClub { get; set; } 
    public virtual Club AwayClub { get; set; } 

    [Required] 
    public int RoundId { get; set; } 

    [ForeignKey("RoundId")] 
    [InverseProperty("Matches")] 
    public virtual Round Round { get; set; } 

} 

public class Context : DbContext 
{ 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<Match>() 
.HasRequired(m => m.HomeClub) 
.WithMany(m => m.HomePlays) 
.HasForeignKey(m => m.HomeClubId) 
.WillCascadeOnDelete(false); 

     modelBuilder.Entity<Match>() 
      .HasRequired(m => m.AwayClub) 
      .WithMany(m => m.AwayPlays) 
      .HasForeignKey(m => m.AwayClubID) 
      .WillCascadeOnDelete(false); 
    } 
} 

Und dann gibt es die erzeugte MatchesController mit Aktionen Index und erstellen :

Ansicht 10
public class MatchesController : Controller 
{ 
    private ApplicationDbContext db = new ApplicationDbContext(); 

    // GET: Matches 
    public ActionResult Index() 
    { 
     var match = db.Match.Include(m => m.Round); 
     return View(match.ToList()); 
    } 

    // GET: Matches/Create 
    public ActionResult Create() 
    { 
     ViewBag.RoundId = new SelectList(db.Round, "RoundID", "RoundID"); 
     return View(); 
    } 

    // POST: Matches/Create 
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create([Bind(Include = "MatchId,HomeClubId,AwayClubID,Club1Goals,Club2Goals,RoundId")] Match match) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Match.Add(match); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     ViewBag.RoundId = new SelectList(db.Round, "RoundID", "RoundID", match.RoundId); 
     return View(match); 
    } 
} 

Und das Erstellen:

@model ProjSty3Exp.Models.Match 

@{ 
ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 

@using (Html.BeginForm()) 
{ 
@Html.AntiForgeryToken() 

<div class="form-horizontal"> 
    <h4>Match</h4> 
    <hr /> 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
    <div class="form-group"> 
     @Html.LabelFor(model => model.HomeClubId, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.HomeClubId, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.HomeClubId, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.AwayClubID, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.AwayClubID, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.AwayClubID, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.RoundId, "RoundId", htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.DropDownList("RoundId", null, htmlAttributes: new { @class = "form-control" }) 
      @Html.ValidationMessageFor(model => model.RoundId, "", 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> 

Antwort

0

in Get Aktionsmethode

public ActionResult Create() 
    { 
     ViewBag.RoundId = new SelectList(db.Round, "RoundID", "RoundID"); 
     ViewBag.AwayClubID = new SelectList(db.Clubs, "ClubID", "ClubID"); 
     ViewBag.HomeClubId = new SelectList(db.Clubs, "ClubID", "ClubID"); 
     return View(); 
    } 

In Rasierapparat Ansicht

ersetzen

@Html.EditorFor(model => model.HomeClubId, new { htmlAttributes = new { @class = "form-control" } }) 

von

@Html.DropDownList("HomeClubId", null, htmlAttributes: new { @class = "form-control" }) 

ersetzen

@Html.EditorFor(model => model.AwayClubID, new { htmlAttributes = new { @class = "form-control" } }) 

von

@Html.DropDownList("AwayClubID", null, htmlAttributes: new { @class = "form-control" }) 

Beitrag Aktion Methode

// POST: Matches/Create 
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create([Bind(Include = "MatchId,HomeClubId,AwayClubID,Club1Goals,Club2Goals,RoundId")] Match match) 
{ 
    if (ModelState.IsValid) 
    { 
     db.Match.Add(match); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    ViewBag.RoundId = new SelectList(db.Round, "RoundID", "RoundID", match.RoundId); 
    ViewBag.AwayClubID = new SelectList(db.Clubs, "ClubID", "ClubID", match.AwayCludId); 
    ViewBag.HomeClubId = new SelectList(db.Clubs, "ClubID", "ClubID", match.HomeClubId); 
    return View(match); 
} 
+0

Vielen Dank, es funktioniert! –

Verwandte Themen