无法发布所选值下拉列表

时间:2016-04-06 16:51:02

标签: c# asp.net-mvc razor

我在将document.getElementById('selectBox').selectedIndex = 0 中的值发布到控制器时遇到了一些问题我收到此错误:

  

“没有类型'IEnumerable'的ViewData项具有'TipoDespesa'键。”

我是Asp.net Mvc的新手,我不知道如何解决这个问题。我这样做了:

模型:

BeginForm

我填写下拉菜单的控制器:

@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>

            }

我执行查询的控制器

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

0 个答案:

没有答案