1
我有一個C#類Team.cs.當我創建了一個團隊我填的是事件列表中這樣做:每個新類的實例列表
team.Events = db.Events.Where(sc => sc.SchoolcupEvent == true).ToList();
它正常工作,如果我只創建一個團隊,但是當我創建第二個團隊,它看起來像一線隊的事件列表是空的再次,有誰知道爲什麼會發生這種情況?
你可以在下面找到我的teamcreate的代碼。
Team.cs:
public class Team {
[Key]
[Required(ErrorMessage = "A name is required")]
public string TeamId { get; set; }
[DisplayName("Photo")]
public string Photo { get; set; }
[Display(Name = "Logo")]
public string Logo { get; set; }
[Required(ErrorMessage = "A sports type is required")]
public string Sport { get; set; }
public int CoachId { get; set; }
[ForeignKey("CoachId")]
public virtual Coach Coach { get; set; }
public string SchoolId { get; set; }
[ForeignKey("SchoolId")]
public virtual School School { get; set; }
public double Score { get; set; }
public List<Event> Events { get; set; }
[Required(ErrorMessage="A gender category is required")]
public string Gender { get; set; }
}
創作團隊:
public ActionResult Create(Team team, IEnumerable<HttpPostedFileBase> files)
{
Coach loggedCoach = new DBUserFinder().getLoggedCoach();
team.CoachId = loggedCoach.CoachId;
team.SchoolId = loggedCoach.SchoolId;
team.Events = db.Events.Where(sc => sc.SchoolcupEvent == true).ToList();
ViewBag.Type = new SelectList(db.Sports, "SportName", "SportName");
if (ModelState.IsValid)
{
int i = 0;
foreach (HttpPostedFileBase file in files)
{
if (file != null && file.ContentLength > 0)
{
string fileName = Path.GetFileName(file.FileName);
var extention = Path.GetExtension(file.FileName);
var allowedExtensions = new[] { ".png", ".jpg", ".jpeg" };
if (allowedExtensions.Contains(extention))
{
fileName = team.TeamId.ToString();
String fullPath = "";
if (i == 0)
{
fullPath = fileName + " photo" + extention;
}
if (i == 1)
{
fullPath = fileName + " logo" + extention;
}
var path = Path.Combine(Server.MapPath("~/Content/Images/IO/Team"), fullPath);
file.SaveAs(path);
if (i == 0)
{
team.Photo = "../../Content/Images/IO/Team/" + team.TeamId + " photo" + extention;
}
if (i == 1)
{
team.Logo = "../../Content/Images/IO/Team/" + team.TeamId + " logo" + extention;
}
}
i++;
}
}
db.Teams.Add(team);
db.SaveChanges();
return RedirectToAction("Index");
}