【发布时间】:2015-01-14 12:57:54
【问题描述】:
我有一个“部分”工作代码,其中包含团队和玩家(以及连接表 TeamPlayers)之间的多对多关系。 Team 和 Player 端的下拉菜单不起作用。虽然数据存储在 db(Player 和 Team 表)中,但不存储在连接表 TeamPlayers 中。
我希望在我的视图中能够连接当前玩家和当前团队,也就是在我的联结表中将当前 TeamId 保存到 TeamId(并将 PlayerId 保存到 PlayerId)。最后插入步骤是....我完全卡住了.... 感谢各种帮助。请参阅下面的我的代码,并在必要时询问更多信息。提前致谢! /那只猪
模型 m2mContext.cs
public class m2mContext : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, please use data migrations.
// For more information refer to the documentation:
// http://msdn.microsoft.com/en-us/data/jj591621.aspx
public m2mContext() : base("name=m2mContext")
{
}
public System.Data.Entity.DbSet<m2m.Models.Team> Teams { get; set; }
public System.Data.Entity.DbSet<m2m.Models.Player> Players { get; set; }
}
Model Team.cs
namespace m2m.Models
{
public class Team
{
public int TeamId { get; set; }
[Required]
public string TeamName { get; set; }
public int PlayerId { get; set; }
public virtual ICollection<Player> Players { get; set; } // This is new
}
}
模型播放器.cs
namespace m2m.Models
{
public class Player
{
public int PlayerId { get; set; }
public string PlayerName { get; set; }
public int TeamId { get; set; }
// public virtual Team Team { get; set; } // This is new
public virtual ICollection<Team> Teams { get; set; }
}
}
控制器 TeamController.cs
namespace m2m.Controllers
{
public class TeamController : Controller
{
private m2mContext db = new m2mContext();
// GET: Team
public ActionResult Index()
{
return View(db.Teams.ToList());
}
// GET: Team/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Team team = db.Teams.Find(id);
if (team == null)
{
return HttpNotFound();
}
return View(team);
}
// GET: Team/Create
public ActionResult Create()
{
return View();
}
// POST: Team/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 = "TeamId,TeamName,PlayerId")] Team team)
{
if (ModelState.IsValid)
{
db.Teams.Add(team);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(team);
}
控制器 PlayerController.cs
namespace m2m.Controllers
{
public class PlayerController : Controller
{
private m2mContext db = new m2mContext();
// GET: Player
public ActionResult Index()
{
return View(db.Players.ToList());
}
// GET: Player/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Player player = db.Players.Find(id);
if (player == null)
{
return HttpNotFound();
}
return View(player);
}
// GET: Player/Create
public ActionResult Create()
{
return View();
}
// POST: Player/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 = "PlayerId,PlayerName,TeamId")] Player player)
{
if (ModelState.IsValid)
{
db.Players.Add(player);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(player);
}
查看团队
创建
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Team</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.TeamName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TeamName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TeamName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PlayerId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PlayerId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PlayerId, "", 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>
}
查看播放器
创建
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Player</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.PlayerName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PlayerName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PlayerName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TeamId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TeamId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TeamId, "", 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>
}
【问题讨论】:
标签: c# entity-framework asp.net-mvc-5 many-to-many