FernandoPaiva
FernandoPaiva

Reputation: 4460

How to use DropDownListFor so select a multiple values?

I have a Model and in this Model I created a List<SelectListItem> to select multiple values, on HTML these values are showing. These values are coming of a IList<FormaPagamento>. So, now, I need to add a multiple values in my model but I can't do this, I'm selecting the multiple values but when I do the submit the List<SelectListItem> is empty.

How could I do this ?

trying.

Model

public class EmpresaModel{
    [Required(ErrorMessage="Informe ao menos uma forma de pagamento disponível")]
    public List<SelectListItem> formasPagto { get; set; }
}

Controller

private List<SelectListItem> getFormasPagto() {
    IList<FormaPagamento> lista = fpDAO.findAll();
    List<SelectListItem> dropDown = new List<SelectListItem>();
    foreach (FormaPagamento x in lista) {
        dropDown.Add(new SelectListItem { Text = x.descricao, Value = Convert.ToString(x.id)});
    }
    return dropDown;
}

public ActionResult add() {
    EmpresaModel model = new EmpresaModel();
    model.formasPagto = getFormasPagto();
    return View(model);
}

public JsonResult addAjax(EmpresaModel model) {     
   Debug.WriteLine("Formas Pagto: " + model.formasPagto.Count);
   return Json(jsonResposta);
}

HTML

@model EmpresaModel
<div class="form-group">
    <label for="name" class="cols-sm-2 control-label">Formas de pagamento disponíveis <img src="~/Imagens/required.png" height="6" width="6"></label>
    @Html.DropDownListFor(model => Model.formasPagto, Model.formasPagto, new { Class = "form-control", placeholder = "Selecione as formas de pagamento disponíveis", @multiple = true})
    @Html.ValidationMessageFor(model => Model.formasPagto)
</div>

Upvotes: 1

Views: 708

Answers (1)

user3559349
user3559349

Reputation:

formasPagto is IEnumerable<SelectListItem> - you cannot bind a <select> to a collection of complex objects. A <select multiple> only posts back an array of simple values (the values of the selected options).

You model needs a property to bind to, say

[Required(ErrorMessage="Informe ao menos uma forma de pagamento disponível")]
public IEnumerable<int> SelectedItems { get; set; }

assuming the id property of FormaPagamento is typeof int. Note also that the [Required] attribute is applied to this property, not the SelectList.

.And to create a <select multiple> you use LstBoxFor(), not DropDownListFor()

<label for="@Html.IdFor(m.SelectedItems)" class="cols-sm-2 control-label">Formas de pagamento disponíveis
    <img src="~/Imagens/required.png" height="6" width="6">
</label>
@Html.ListBoxFor(m => m.SelectedItems, Model.formasPagto, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.SelectedItems)

Side note: You can simply use

model.formasPagto = new SelectList(fpDAO.findAll(), "id", "descricao");

or

model.formasPagto = fpDAO.findAll().Select(x => new SelectListItem
{
    Value = x.id.ToString(),
    Text = descricao
});

to generate the SelectList

Upvotes: 2

Related Questions