Я не был точно уверен, как сформулировать вопрос, но у меня есть кусок C# кода для задания колледжа. Я должен в основном создать приложение, которое показывает данные из базы данных. Я довольно далеко, но у меня есть этот последний фрагмент кода, с которым я в основном борюсь.
Прежде чем я покажу вам свой код, позвольте мне быстро суммировать, в чем проблема: моя комбинация Модель / Представление регистрирует оба дома. и команды Away одинаковые, из-за возможной ошибки в моем коде, из-за которой система не смогла разделить их и показать значения, независимые от друг друга.
Это мой контроллер.
public ActionResult Index()
{
// create result variable to later on pass it to the view.
List<MatchDetails> result = new List<MatchDetails>();
foreach (MatchStats item in db.MatchStats)
{
// create new MatchDetails instance
MatchDetails matchDetails = new MatchDetails();
// make the hometeamID from this instance equal to the ID value from the database
matchDetails.HomeTeamId = item.Home_Team_ID;
// look for nation (country) that is equivalent to the hometeamID
Nation HomeTeam = db.Nation.Single(n => n.Nation_ID == matchDetails.HomeTeamId);
//make a new Team instance
Team homeTeam = new Team();
//make the team name of the hometeam equal to the name of the country
homeTeam.TeamName = HomeTeam.Country;
// get matchDetails property, get the HomeTeamFlag property and set it equal to the Flag property in the database
homeTeam.TeamFlag = HomeTeam.Flag;
//make home team property equal to the homeTeam variable
matchDetails.HomeTeam = homeTeam;
matchDetails.AwayTeamId = item.Away_Team_ID;
Nation AwayTeam = db.Nation.Single(n => n.Nation_ID == matchDetails.AwayTeamId);
Team awayTeam = new Team();
awayTeam.TeamName = AwayTeam.Country;
awayTeam.TeamFlag = AwayTeam.Flag;
matchDetails.AwayTeam = awayTeam;
matchDetails.HomeTeamGoals = item.GoalsHomeTeam;
matchDetails.AwayTeamGoals = item.GoalsAwayTeam;
//add the matchdetails item(s) to the result
result.Add(matchDetails);
}
// pass all results to the view.
return View(result);
}
public ActionResult Details(int id)
{
// Here we create a new MatchDetails object (and through the constructor we also create the sub objects such as HomeTeamStats)
MatchDetails matchDetails = new MatchDetails();
// make the hometeamID from this instance equal to the ID value from the database
matchDetails.HomeTeamId = id;
matchDetails.AwayTeamId = id;
// look for nation (country) that is equivalent to the hometeamID
Nation homeTeam = db.Nation.Single(n => n.Nation_ID == id);
Nation awayTeam = db.Nation.Single(n => n.Nation_ID == id);
// this is where you retrieve the real amount of red cards given from the database.
// fetch the row in question from the database and connect it to the relevant table
matchDetails.HomeTeamStats.RedCardsHomeTeam = homeTeam.MatchStats.Sum(m => m.RedCardsHomeTeam);
matchDetails.HomeTeamStats.YellowCardsHomeTeam = homeTeam.MatchStats.Sum(m => m.YellowCardsHomeTeam);
matchDetails.AwayTeamStats.RedCardsAwayTeam = awayTeam.MatchStats.Sum(m => m.RedCardsAwayTeam);
matchDetails.AwayTeamStats.RedCardsAwayTeam = awayTeam.MatchStats.Sum(m => m.YellowCardsAwayTeam);
// Inputting dummy value that we want to test with in our view
//matchDetails.HomeTeamStats.RedCardsHomeTeam = 10; // the value of 10 is a dummy value, we now have to extract the real value from the database.
NationPlayerList nationPlayerList = new NationPlayerList(); // new table instance
//nationPlayerList.Players = db.MatchStats.Where(match => match.Match_ID == id).ToList(); // connect nation_ID to page ID
nationPlayerList.Nation = db.Nation.Single(n => n.Nation_ID == id); // show single instance (by ID)
//return View(nationPlayerList); // show created instance
return View(matchDetails);
}
Ниже вы видите модель, сопровождающую контроллер, упомянутый выше.
public class MatchDetails
{
//the constructor. here we create the sub objects so that these cannot be null (this can cause errors)
public MatchDetails()
{
// we create a new object for HomeTeamStats (of type MatchStats)
HomeTeamStats = new MatchStats();
AwayTeamStats = new MatchStats();
YellowCardsHomeTeam = new MatchStats();
RedCardsHomeTeam = new MatchStats();
YellowCardsAwayTeam = new MatchStats();
RedCardsAwayTeam = new MatchStats();
// create the other sub objects here as well (only once is sufficient)
// YellowCardsHomeTeam
// RedCardsHomeTeam
// etc
// etc
}
public Team HomeTeam { get; set; }
public int HomeTeamId { get; set; }
public Team AwayTeam { get; set; }
public int AwayTeamId { get; set; }
public int HomeTeamGoals { get; set; }
public int AwayTeamGoals { get; set; }
public MatchStats YellowCardsHomeTeam { get; set; }
public MatchStats RedCardsHomeTeam { get; set; }
public MatchStats YellowCardsAwayTeam { get; set; }
public MatchStats RedCardsAwayTeam { get; set; }
public MatchStats AwayTeamStats { get; set; }
public MatchStats HomeTeamStats { get; set; }
}
}
Эти фрагменты были переведены мной с голландского на английский sh, поэтому, если я уйду на каком-либо голландском, я приношу свои извинения. Я подумал, что всегда лучше спросить, чем передать мне ответ, не зная, как в следующий раз.