2

I'm having some trouble posting the value that I have in a BeginForm to the controller I got this error:

"There is no ViewData item of type 'IEnumerable' that has the key 'TipoDespesa'."

I'm kinda new in Asp.net Mvc and I don't know how to solve this. I did this:

model:

@using (Html.BeginForm())
            {
                <div class="form-group">
                    <a href="@Url.Action("Create", "Despesas")" class="btn btn-danger" data-toggle="tooltip" title="Adiciona uma despesa" style="float: left;">
                        <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
                    </a>
                </div>
                <div class="form-group col-md-offset-1">
                    <label class="control-label">Tipo de despesa</label>
                    @Html.DropDownList("TipoDespesa", (SelectList)ViewData["TipoDespesa"], "Todos", htmlAttributes: new { @class = "form-control" })
                </div>
                    <div class="form-group"style="padding-left:20px">
                        <label class="control-label">Data</label>
                        <input type="date" class="form-control" name="pesquisa" />
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn btn-default" data-toggle="tooltip" title="Pesquisar por data"><span class="glyphicon glyphicon-search"></span></button>
                    </div>

            }

Controller where i fill the dropdown:

public ActionResult Index()
    {
        var userId = User.Identity.GetUserId();
        var despesas = db.Despesas.Include(d => d.TipoDespesa).Include(d => d.TipoPagamento).Where(d => d.ApplicationUserId == userId);
        ViewBag.TipoDespesa = new SelectList(db.TipoDespesas, "TipoDespesaId", "TipoDespesaNome");

        return View(despesas.ToList());
    }

Controller where i execute the query

 public ActionResult Index(string pesquisa,string tipoDespesa)
    {
        var userId = User.Identity.GetUserId();
        var resultado = from r in db.Despesas.Include(r => r.TipoDespesa).Include(r => r.TipoPagamento).Where(r => r.ApplicationUserId == userId)
                        select r;

        if (!String.IsNullOrEmpty(pesquisa))
        {
            DateTime d = Convert.ToDateTime(pesquisa);
            resultado = resultado.Where(r => r.Data == d);
        }

        if(pesquisa != "Todos")
        {
            resultado.Where(r => r.TipoDespesa.TipoDespesaNome == pesquisa);
        }

        return View(resultado);
    }
Jasen
  • 14,030
  • 3
  • 51
  • 68
  • 1
    You assigned the values in your controller to `ViewBag` but try to access the values from `ViewData`. Change one of these so they match. – Jasen Apr 06 '16 at 16:57
  • 3
    My humble suggestion is to switch to a more strongly typed solution . ViewData is kinda ugly. Here is an example how to use a view model to transfer your dropdown data from your action method to the view. http://stackoverflow.com/questions/34300916/dropdownlist-from-ienumerable/34301135#34301135 – Shyju Apr 06 '16 at 16:59
  • Jasen can you explain what you mean? –  Apr 06 '16 at 17:53
  • He is saying that (SelectList)ViewData["TipoDespesa"] is not the same as ViewBag.TipoDespesa = new SelectList(db.TipoDespesas, "TipoDespesaId", "TipoDespesaNome"); – Nikki9696 Apr 06 '16 at 18:07
  • i used tryed the null but still doesnt work –  Apr 06 '16 at 22:05
  • You cannot use the same name for the property your binding to and the `SelecteList`. And the error occurs because you return the view in your POST method, but have not repopulated the `SelectList` (as you did in the GET method) so its `null` –  Apr 06 '16 at 22:11
  • im not understanding, if i populate my viewbag in my get Index and use it on the post method he actually passed already in the index get and populated the viewbag :S –  Apr 06 '16 at 22:14
  • @FilipeCosta, Suggest your read [this question/answer](http://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o). Its a slightly different error message because of your use of `DropDownList()` instead of `DropDownListFor()` but its the same issue. –  Apr 06 '16 at 22:25
  • `ViewData["TipoDespesa"]` is the same with `ViewBag.TipoDespesa`. See this [SO POST](http://stackoverflow.com/a/4705446/1504480). The real problem here is that you should generate again the `ViewBag.TipoDespesa` on the post since the lifetime of a ViewBag is on the current request only – Bon Macalindong Apr 07 '16 at 09:10

0 Answers0