Este problema ja nao e novidade mas para mim ainda me causa muito transtorno...
no meu controller
public ActionResult Create()
{
ViewBag.ArtigoFK = new SelectList(db.Artigo, "ArtigoID", "nome");
ViewData["ArtigoFK"] = new SelectList(db.Artigo.ToList(), "ArtigoID", "nome");
return View();
}
public ActionResult Create([Bind(Include = "nome,descricao,tipo,ArtigoFK")] Imagens imagens, HttpPostedFile file)
{
if (ModelState.IsValid)
{
imagens.directorio = Path.Combine(Server.MapPath("~/Images"),Path.GetFileName(imagens.nome));
db.Imagem.Add(imagens);
db.SaveChanges();
}
if (file != null && file.ContentLength > 0)
try {
string path = Path.Combine(Server.MapPath("~/Images"),
Path.GetFileName(file.FileName));
file.SaveAs(path);
ViewBag.Message = "File uploaded successfully";
} catch (Exception ex) {
ViewBag.Message = "ERROR:" + ex.Message.ToString();
} else {
ViewBag.Message = "You have not specified a file.";
}
ViewBag.ArtigoFK = new SelectList(db.Artigo.ToList(), "ArtigoID", "nome");
ViewData["ArtigoFK"] = new SelectList(db.Artigo.ToList(), "ArtigoID", "nome");
return View();
}
na minha view
<div class="form-group">
@Html.LabelFor(model => model.ArtigoFK, "ArtigoFK", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ArtigoFK", (IEnumerable<SelectList>) ViewBag.ArtigoFK, new { ArtigoID = "ArtigoFK" })
@Html.ValidationMessageFor(model => model.ArtigoFK, "", new { @class = "text-danger" })
</div>
</div>
da o erro
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key
ja tentei de tudo para dar certo e nada...