I'm struggling to solve a problem in relation to foreign keys.
Scenario: I'm building a very basic wrestling simulator game.
Some of the model classes that I have are:
public class Wrestler
{
public int WrestlerId { get; set; }
public string Name { get; set; }
public int Overall { get; set; }
public string Finisher { get; set; }
public virtual ICollection<Match> Matches { get; set; }
}
public class Show
{
public int ShowId { get; set; }
public string Name { get; set; }
public int PromotionId {get; set;}
public int MatchId { get; set; }
public virtual ICollection<Match> Matches { get; set; }
public virtual Promotion Promotion { get; set; }
}
public class Promotion
{
public int PromotionId { get; set; }
public string Name { get; set; }
public decimal Budget { get; set; }
public string Size { get; set; }
public virtual ICollection<Championship> Championship { get; set; }
}
public class Championship
{
public int ChampionshipId { get; set; }
public string Name { get; set; }
public string Prestige { get; set; }
public int PromotionId { get; set; }
public virtual Promotion Promotion { get; set; }
}
The problem: I would like to add the functionality to create a Wrestling Match using a simple drop-down style form to select two wrestlers to face off and also to decide a winner, however, I can't figure out how to do this.
Here is what I have so far.
public class Match
{
public int MatchId { get; set; }
public int WrestlerId { get; set; }
public int WrestlerTwoId { get; set; }
public int WinnerId { get; set; }
public int ShowId { get; set; }
public virtual Wrestler Wrestler { get; set; }
public virtual Show Show { get; set; }
}
WrestlerId should be for example Hulk Hogan (WrestlerId = 1), and WrestlerTwoId should be "The Rock" (WrestlerTwoId = 2) and let's say Hulk Hogan is the winner so (WinnerId=1)
So how do I build a drop-down, like:
Here is the create view, Note, I know it may be incorrect
<div class="form-horizontal">
<h4>Match</h4>
<div class="form-group">
<label class="control-label col-md-2" for="Wrestler">Wrestler</label>
<div class="col-md-10">
@Html.DropDownList("WrestlerId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.WrestlerId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="Wrestler">Wrestler</label>
<div class="col-md-10">
@Html.DropDownList("WrestlerTwoId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.WrestlerTwoId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="Winner">Winner</label>
<div class="col-md-10">
@Html.DropDownList("WinnerId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.WinnerId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="Show">Show</label>
<div class="col-md-10">
@Html.DropDownList("ShowId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ShowId, "", new { @class = "text-danger" })
</div>
</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>
And here is the create action on the match controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "MatchId,WrestlerId,WrestlerTwoId,WinnerId,ShowId")]Match match)
{
try
{
if (ModelState.IsValid)
{
db.Matches.Add(match);
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (RetryLimitExceededException /* dex */)
{
//Log the error (uncomment dex variable name and add a line here to write a log.)
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
PopulateShowsDropDownList(match.ShowId);
return View(match);
}
Also if needed, here is the context class:
public class WrestlingContext : DbContext
{
public DbSet<Wrestler> Wrestlers { get; set; }
public DbSet<Promotion> Promotions { get; set; }
public DbSet<Championship> Championships { get; set; }
public DbSet<Match> Matches { get; set; }
public DbSet<Show> Shows { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
I have searched high and low, with this answer being the closest to what I thought I needed, but it didn't seem to work.
I really appreciate the feedback as this is a difficult one for me to solve.