0
我很新的MVC和搜索和檢查多個解決方案,但沒有爲我工作。asp.net MVC - 代碼第一外鍵讓我輸入任何東西
我有一個使用Code First製作的數據庫。它有3個表格:俱樂部,比賽和回合。每一輪都有幾場比賽。在每場比賽中打2個俱樂部。 Match包含Round的一個外鍵(這很好用)和兩個單獨的Match(主隊和客隊)的多個鍵。
當我使用EF添加MatchesController與視圖並啓動應用程序並嘗試輸入新的匹配時,我有一個從數據庫的下拉列表 - 作品令人驚歎。我想從數據庫中獲得兩個有團隊名稱/ ID的下拉列表,所以我可以選擇參加比賽的球隊,但出於某種原因,我有兩個文本框讓我寫任何東西,就好像FK約束不存在一樣。
我有一個相當的問題,因爲我不知道我在做什麼錯 - 是外鍵?我在瀏覽其他問題時將它編碼爲這樣,但也許我搞砸了?
回合類:
public class Round : IValidatableObject
{
public int RoundID { get; set; }
[Required]
public int RoundNumber { get; set; }
public virtual ICollection<Match> Matches { get; set; }
}
俱樂部等級:
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; }
}
匹配類及其DB上下文類:
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);
}
}
再有就是用行動指標和創建產生MatchesController :
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);
}
}
而創建視圖:
@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>
非常感謝你,它的工作原理! –