(自定义ViewBag与Where)。简单的线条,但我不能让它工作

时间:2016-06-17 21:36:40

标签: c# asp.net-mvc asp.net-mvc-5 viewbag

ViewBag.cliente_id = new SelectList(db.cliente.Where(c => c.status_ativacao == 1), "id", "nome", manutencao.cliente_id);

它仍在返回所有客户。 我需要回复哪一栏' status_ativacao' =值1。 我确定在这里这样做或者我需要在下拉列表中这样做吗?

查看:

<div class="form-group">
<label class="col-sm-2 control-label" for="cliente_id">Cliente</label>
<div class="col-md-6">
    @Html.DropDownList("cliente_id", (SelectList)ViewBag.Values, new { onchange = "this.form.submit();" })
    @Html.ValidationMessageFor(model => model.cliente_id)
 <!--   <input type="submit" value="Atualizar Cliente" class="btn btn-default"/> -->
</div>

1 个答案:

答案 0 :(得分:1)

您必须先将该查询转换为列表,如下所示:

ViewBag.cliente_id = new SelectList(db.cliente.Where(c => c.status_ativacao == 1).ToList(), "id", "nome", manutencao.cliente_id);

另外,在视图中更改Html.DropDownList,如下所示:

@Html.DropDownList("cliente_id", (SelectList)ViewBag.cliente_id, new { onchange = "this.form.submit();" })