Flávio Augusto
Flávio Augusto

Reputation: 79

(Custom ViewBag with Where). Simple line but I cant make it work

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

It Still are returning all clients. I need to return just who column 'status_ativacao' = value 1. Am I sure doing this here or I need to do it on dropdownlist?

View:

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

Upvotes: 1

Views: 69

Answers (1)

Carlos Figueroa
Carlos Figueroa

Reputation: 1823

You have to convert that query into a list first, like this:

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

Also, change the Html.DropDownList in your view, like this:

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

Upvotes: 1

Related Questions