我使用带有Entity Framework的asp.net MVC创建了一个网页。我想在查看中返回所选项目。如果选择多个数据,则仅返回最后一个项目。当我调试时,数组会接收选定的项目,但是在foreach循环中,仅返回最后一个查询。我该如何解决?
@using (Html.BeginForm())
{
@Html.DropDownList("prof_id", null, htmlAttributes: new { @class = "form-control", @multiple = "multiple" })<br /><br />
<input type="submit" value="Search" />
}
public ActionResult Index(int[] prof_id)
{
ViewBag.prof_id = new MultiSelectList(db.prof, "prof_id", "name");
List<user> test = new List<user>();
foreach (var item in prof_id)
{
test = db.user.Where(u => u.prof_id == item).ToList();
}
return View(test.ToList());
}
public partial class prof
{
public prof()
{
this.user = new HashSet<user>();
}
public int prof_id { get; set; }
public string name { get; set; }
public virtual ICollection<user> user { get; set; }
}
答案 0 :(得分:1)
在其中更改foreach循环,
var tempList = db.user.Where(u => u.prof_id == item).ToList();
test.AddRange(tempList);
希望有帮助,