0

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:

enter image description here

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vamprio
  • 1
  • 1
  • You need 3 `Wrestler` navigation properties, one for each FK. Although having two FKs to the same `Wrestler` seems weird. Why not have a winner FK and a loser FK instead and only 2 drop downs instead of 3? Also I don't think you want a `MatchId` in `Show` since a `Show` has many `Matches` and not just one. – juharr Jul 04 '20 at 15:11
  • @juharr so WinnerId and LoserId , I will try this out, however let's say I had multiple winners and multiple losers, would this be harder to do?. Also thanks for your response, I appreciate it – Vamprio Jul 04 '20 at 19:45
  • In that case you'd want a many-to-many table between Match and Wrestler. That table would contain a FK MatchID, FK WrestlerID and a column indicating if they won or lost. The down side is there is no constraint on the number of winners or loser or even how many wrestlers are in a given match, but has the flexibility to handle one on one matches, tag team matches, and rumbles versus having separate tables for each. – juharr Jul 04 '20 at 22:10
  • @juharr is there any downsides with not having constraints? Or is it acceptable on what your proposed due to a match being a different setup i.e (tag, one on one etc) ? Also then if say this will a many to many table, how would I associate this with a number if different matches with a show? – Vamprio Jul 05 '20 at 06:05

0 Answers0