C# ASP.NET MVC实体框架使用MultiSelectList插入多对多表
我有三张桌子: 投票:C# ASP.NET MVC实体框架使用MultiSelectList插入多对多表,c#,asp.net-mvc,entity-framework,many-to-many,C#,Asp.net Mvc,Entity Framework,Many To Many,我有三张桌子: 投票: public int PollId { get; set; } public string PollTitle { get; set; } public virtual ICollection<Party> Parties { get; set; } private VoterEntities db = new VoterEntities(); // // GET: /Polls/Create public Action
public int PollId { get; set; }
public string PollTitle { get; set; }
public virtual ICollection<Party> Parties { get; set; }
private VoterEntities db = new VoterEntities();
//
// GET: /Polls/Create
public ActionResult Create()
{
ViewBag.PartyId = new MultiSelectList(db.Parties, "PartyId", "PartyName");
return View();
}
//
// POST: /Polls/Create
[HttpPost]
public ActionResult Create(Poll poll)
{
//var parti = db.Parties.Find(partyId);
if (ModelState.IsValid)
{
var pList = new List<Party>();
foreach (var pId in "PartyId")
{
pList.Add(new Party
{
PartyId = pId
});
}
poll.Parties = pList;
db.Polls.Add(poll);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.PartyId = new MultiSelectList(db.Parties, "PartyId", "PartyName", poll.Parties);
return View(poll);
}
@model VoterMVC.Models.Poll
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.PollTitle)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PollTitle)
@Html.ValidationMessageFor(model => model.PollTitle)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Parties)
</div>
<div class="editor-field">
@Html.ListBox("PartyId")
@Html.ValidationMessageFor(model => model.Parties)
</div>
<p>
<input type="submit" value="Create" />
</p>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
在我的PollController中:
public int PollId { get; set; }
public string PollTitle { get; set; }
public virtual ICollection<Party> Parties { get; set; }
private VoterEntities db = new VoterEntities();
//
// GET: /Polls/Create
public ActionResult Create()
{
ViewBag.PartyId = new MultiSelectList(db.Parties, "PartyId", "PartyName");
return View();
}
//
// POST: /Polls/Create
[HttpPost]
public ActionResult Create(Poll poll)
{
//var parti = db.Parties.Find(partyId);
if (ModelState.IsValid)
{
var pList = new List<Party>();
foreach (var pId in "PartyId")
{
pList.Add(new Party
{
PartyId = pId
});
}
poll.Parties = pList;
db.Polls.Add(poll);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.PartyId = new MultiSelectList(db.Parties, "PartyId", "PartyName", poll.Parties);
return View(poll);
}
@model VoterMVC.Models.Poll
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.PollTitle)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PollTitle)
@Html.ValidationMessageFor(model => model.PollTitle)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Parties)
</div>
<div class="editor-field">
@Html.ListBox("PartyId")
@Html.ValidationMessageFor(model => model.Parties)
</div>
<p>
<input type="submit" value="Create" />
</p>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
private voterenties db=new voterenties();
//
//获取/查询/创建
公共操作结果创建()
{
ViewBag.PartyId=新的多选列表(db.Parties,“PartyId”,“PartyName”);
返回视图();
}
//
//POST:/Polls/创建
[HttpPost]
公共行动结果创建(投票)
{
//var parti=db.Parties.Find(partyId);
if(ModelState.IsValid)
{
var pList=新列表();
foreach(在“PartyId”中的var pId)
{
pList.Add(新党)
{
PartyId=pId
});
}
投票。政党=普利斯特;
db.Polls.Add(poll);
db.SaveChanges();
返回操作(“索引”);
}
ViewBag.PartyId=新的多选列表(db.Parties,“PartyId”,“PartyName”,poll.Parties);
返回视图(投票);
}
我的观点:
public int PollId { get; set; }
public string PollTitle { get; set; }
public virtual ICollection<Party> Parties { get; set; }
private VoterEntities db = new VoterEntities();
//
// GET: /Polls/Create
public ActionResult Create()
{
ViewBag.PartyId = new MultiSelectList(db.Parties, "PartyId", "PartyName");
return View();
}
//
// POST: /Polls/Create
[HttpPost]
public ActionResult Create(Poll poll)
{
//var parti = db.Parties.Find(partyId);
if (ModelState.IsValid)
{
var pList = new List<Party>();
foreach (var pId in "PartyId")
{
pList.Add(new Party
{
PartyId = pId
});
}
poll.Parties = pList;
db.Polls.Add(poll);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.PartyId = new MultiSelectList(db.Parties, "PartyId", "PartyName", poll.Parties);
return View(poll);
}
@model VoterMVC.Models.Poll
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.PollTitle)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PollTitle)
@Html.ValidationMessageFor(model => model.PollTitle)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Parties)
</div>
<div class="editor-field">
@Html.ListBox("PartyId")
@Html.ValidationMessageFor(model => model.Parties)
</div>
<p>
<input type="submit" value="Create" />
</p>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
@model VoterMVC.Models.Poll
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true)
@LabelFor(model=>model.PollTitle)
@EditorFor(model=>model.PollTitle)
@Html.ValidationMessageFor(model=>model.PollTitle)
@LabelFor(model=>model.Parties)
@Html.ListBox(“PartyId”)
@Html.ValidationMessageFor(model=>model.Parties)
}
@节脚本{
@Scripts.Render(“~/bundles/jqueryval”)
}
现在,这项工作大部分像一个符咒,但foreach循环没有。它不会与所选参与方的视图列表框通信,循环会向参与方表和PartiesInPoll表添加7(?!)新行。
所以我想要的当然是让从视图的ListBox中选择的值通过foreach循环发送,这样PartiesInPoll表就会得到更新。
我不想在聚会表中添加新的聚会
希望你明白我的意思。
这件事已经拖了几天了,非常感谢您的帮助,谢谢
编辑:
如果有更好的解决方案,请说出来:)这段代码看起来是错误的:
foreach (var pId "PartyId")
它甚至不应该编译。也许你有
foreach (var pId in "PartyId")
这段代码迭代“PartyId”字符串中的字符。恰好有7个。一旦您从列表框中选择了一个
PartyId
,视图只向post方法发送这个id。在这种情况下,for循环将变得无用。大多数问题都来自数据库中的多对多关系。请阅读更多关于此的信息。如果您希望在一次投票中有多个参与方创建1对*(或*对1)关系,您会发现这是一种更简单的方法。是的,我粘贴时犯了错误。。!在我的原始代码中是正确的。对此很抱歉。请查看SimpleMembership表
查看成员资格和角色,然后查看UsersInRoles如何操作。如果启用了迁移功能,请尝试对数据进行Seed