0
I have a very strange problem with my dropdown. When I load Submit this error appears:
There is no Viewdata item of type 'Ienumerable' that has the key 'Diocese'.
but I believe that the mistake is not in this Dropdown because I always did this and it always worked.
Controller:
public ActionResult CriarCatequese()
{
//Lista de Dioceses para escolher uma vigararia
ViewBag.Dioceses = new SelectList(db.Diocese, "DioceseID", "Nome");
return View();
}
View:
<div class="form-group">
@Html.Label("Diocese", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-3">
@Html.DropDownList("Diocese", (SelectList)ViewBag.Dioceses, "--Escolha uma diocese--", htmlAttributes: new { @class = "form-control" })
</div>
</div>
I think the problem may be here, that I have to select several parochials in Listbox, the Listbox is filled with an AJAX call, I did this with the following code:
view:
<div class="form-group">
@Html.Label("Paroquias", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-3">
@Html.ListBoxFor(m => m.SelectedParoquias, new MultiSelectList(string.Empty, "ParoquiaID", "Nome"), htmlAttributes: new { @class = "form-control" })
</div>
</div>
Modelview:
public class CatequeseViewModel
{
public IEnumerable<int> SelectedParoquias { get; set; }
//selecionar Paroquias
public Catequese Catequese { get; set; }
public IEnumerable<SelectListItem> TodasParoquias { get; set; }
private List<int> _selecionarParoquias;
public List<int> SelecionarParoquias
{
get
{
if (_selecionarParoquias == null)
{
_selecionarParoquias = Catequese.Paroquias.Select(m => m.ParoquiaID).ToList();
}
return _selecionarParoquias;
}
set { _selecionarParoquias = value; }
}
}
Controller:
public ActionResult CriarCatequese(CatequeseViewModel m)
{
if (ModelState.IsValid)
{
var catequese = new Catequese
{
NomeCatequese = m.Catequese.NomeCatequese,
Morada = m.Catequese.Morada,
Localidade = m.Catequese.Localidade,
CodigoPostal = m.Catequese.CodigoPostal,
Telefone = m.Catequese.Telefone,
email = m.Catequese.email
};
//codigo para adicionar as paroquias
catequese.Paroquias = db.Paroquia.Where(p => m.SelectedParoquias.Contains(p.ParoquiaID)).ToList();
db.Catequese.Add(catequese);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(m);
}
Managed to solve the problem?
– Felipe Assunção
Continues with the same problem. " There is no Viewdata item of type 'Ienumerable<Selectlistitem>' that has the key 'Diocese'."
– Simão Lopes
I think the problem is here
@Html.ListBoxFor(m => m.SelectedParoquias, new MultiSelectList(string.Empty, "ParoquiaID", "Nome"), htmlAttributes: new { @class = "form-control" })
, I have to do a cast, but this is being filled by an ajax call, I don’t know how to do a cast.– Simão Lopes