问题
I have a "partial" working code with many to many relationship between Teams and Players (and junction table TeamPlayers). The drop down menu on Team and Player side does not work.Though data is stored in db (Player and Team tables)BUT NOT to junction table TeamPlayers.
I want to have the possibility in my view for current players and current teams to be connected, aka, save a current TeamId to TeamId in my junction table (and PlayerId to PlayerId). The last insert step is....i´m totally stuck.... Grateful for all kind of help. the See my code below, and ask for more if necessary. Thanks in advance! /Tha Pig
Model 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
}
}
Model Player.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; }
}
}
Controller 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);
}
Controller 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);
}
View Team
Create
@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>
}
View Player
Create
@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>
}
回答1:
I cannot comment due to my rep, so I will post a possible answer.
I would add TeamPlayers to the model, add a method to the players and/or team controller to add them to the TeamPlayer.
public System.Data.Entity.DbSet<m2m.Models.TeamPlayer> TeamPlayer{ get; set; }
In Controller(s)
public ActionResult AddtoTeam(int? Teamid)
public ActionResult AddPlayer(int? Playerid)
Not entirely sure this is what you want.
回答2:
The problem is your posting to properties PlayerId
/TeamId
. This is a M2M so these fields don't do anything. You have to provide a multi-select that will end up posting a list of ids. Then you can use this posted list of ids to lookup the players/teams from the database and add them to their respective collections.
来源:https://stackoverflow.com/questions/27943512/many-to-many-mvc-5-model-first-write-to-join-table