Problem in a dropdown

Asked

Viewed 44 times

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);
        }

1 answer

1

You need to specify that your list is a IEnumerable

Example:

@Html.DropDownList("Diocese", (IEnumerable<SelectListItem>)ViewBag.Dioceses, "--Escolha uma diocese--", htmlAttributes: new { @class = "form-control" })
  • Managed to solve the problem?

  • Continues with the same problem. " There is no Viewdata item of type 'Ienumerable<Selectlistitem>' that has the key 'Diocese'."

  • 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.