任何人都可以帮助我,我不知道我做错了什么。 。 。 在我的控制器上
public ActionResult ProdutoCategoria()
{
var model = db.Perfies;
ViewBag.Perfies_Id = new SelectList(db.Perfies.ToList(), "Id", "NomePerfil");
return View(model);
}
public ActionResult consultaBD(int id)
{
var selectedPerfil = (from Perf in db.Perfies
where Perf.Id == id
select new { Id = Perf.Id, Name = Perf.NomePerfil }).ToList();
Utilizador utili = db.Utilizadores.Find(id);
ViewBag.Perfies_Id = selectedPerfil;
if (utili == null) {
return HttpNotFound();
}
return View();
}
在视图
上<form action="/Produto/consultoDB" method="post">
<label>Select one </label>
@Html.DropDownList("Perfies_Id", new SelectList(ViewBag.Perfies_Id, "Id", "NomePerfil"))
<input type="submit" class="button success" value="Enter" />
</form>
答案 0 :(得分:1)
Perfies_Id
已经是SelectList
。将控制器中的代码更改为
ViewBag.Perfies_Id = db.Perfies.ToList();
或在视图中
@Html.DropDownList("Perfies_Id", ViewBag.Perfies_Id)
答案 1 :(得分:1)
您的选择列表的select
元素名称和ViewBag
变量名称不能相同。他们会发生冲突。将您的ViewBag
变量更改为ViewBag.Perfies_Id_Choices
,您就可以了。