Invoke the selected values in the CheckBox in View Edit

2

I do not know how to return the selected values in checkBox to View .

Controller:

public ActionResult Editar(int? id)
{
    // acedendo a um conjunto de valores de uma classe
    ViewBag.Daylist = GetDias(null);
    return View(inscricao);
}

Auxiliary method:

private MultiSelectList GetDias(string[] selectedValues)
{
    List<DiasSemana> Dias = new List<DiasSemana>()
    {
        new DiasSemana() { ID = 0, Dia= "Domingo" },
        new DiasSemana() { ID = 1, Dia= "Segunda-Feira" },
        new DiasSemana() { ID = 2, Dia= "Terça-Feira" },
        new DiasSemana() { ID = 3, Dia= "Quarta-Feira" },
        new DiasSemana() { ID = 4, Dia= "Quinta-Feira" },
        new DiasSemana() { ID = 5, Dia= "Sexta-Feira" },
        new DiasSemana() { ID = 6, Dia= "Sábado" },
    };
    return new MultiSelectList(Dias, "ID", "Dia", selectedValues);
}

View:

<div class="form-group">
    @Html.LabelFor(model => model.Dias_Preferencial, htmlAttributes: new { @class = "control-label col-md-2" })

    <div class="col-md-3">
        <input type="checkbox" id="allcheck" />
        @Html.Label("Todos", new { @class = "control-label" })  <br />
        @foreach (var item in (MultiSelectList)ViewBag.Daylist)
        {                                
            <input type="checkbox" name="SelectDias" value="@item.Text" class="checkbox-inline" />
            @Html.Label(item.Text, new { @class = "control-label" })
            <br />
        }
    </div>
</div>
    
asked by anonymous 07.04.2016 / 19:45

1 answer

0

This is the bad way to do it. The correct way involves using @Html.ListBox @Html.ListBoxFor :

@Html.ListBoxFor(m => m.SelectDias, (MultiSelectList)ViewBag.Daylist)
    
12.07.2016 / 21:12