Asp.NET MVC checkbox list bind with string value

Asked

Viewed 571 times

1

I have a checkbox list and need to bind values that are strings (@item.Selectedvalue) with my Ienumerable>.

 <div class="editor-field perfil-filtro-expander-todasAcoes">
        <div class="metro perfil-filtro-expander-overflow todasAcoes" id="todasAcoes" name="todasAcoes">
            @foreach (DetailedLookupModel<string> item in (IEnumerable<DetailedLookupModel<string>>)ViewBag.Acoes)
            {                    
                <div style="padding-bottom: 10px;">
                    @*<input id="@item.SelectedValue" type="checkbox" name="@item" value="@item.SelectedValue"/>*@
                    @Html.TextBoxFor(x => Model.Acoes, new { @type = "checkbox", @id = item.SelectedValue, @value = item.SelectedValue})
                    <label for="@item.SelectedValue">
                        <b>@item.Title</b>
                        <br />
                        <span class="perfil-filtro-expander-descricao">
                            @item.Description
                        </span>
                    </label>
                </div>
            }
        </div>
        @Html.ValidationMessageFor(model => model.Descricao)
    </div>

Property of the Model:

[DisplayName("Ações")]
[Required]
public IEnumerable<DetailedLookupModel<String>> Acoes { get; set; }

This way I can get the checboxes, but the value of them was to be the "value" all comes null. Nor can I make changes to the Abels, as my css depends on those ids to understand which are the Abels for the appropriate fields.

  • Worse than already, I tried to use but I found some flaws, it would not be nice I have to change the architecture to have the list of selected items being that I can get through a viewbag with a bank return. Also the "title" field of the helper only accepts a given, I needed to style this value with my css as I already did there. Any solution you know alternative?

  • Enter here: http://chat.stackexchange.com/rooms/25342/c-asp-net-asp-net-mvc-entity-framework-etc

1 answer

2


It’s a classic case of Begincollectionitem.

Do the following:

<div class="editor-field perfil-filtro-expander-todasAcoes">
    <div class="metro perfil-filtro-expander-overflow todasAcoes" id="todasAcoes" name="todasAcoes">
        @foreach (DetailedLookupModel<string> item in (IEnumerable<DetailedLookupModel<string>>)ViewBag.Acoes)
        {                    
            @Html.Partial("_Acoes", item)
        }
    </div>
    @Html.ValidationMessageFor(model => model.Descricao)
</div>

_Acoes.cshtml

@model DetailedLookupModel<String>

@using (Html.BeginCollectionItem("Acoes"))
{
    <div style="padding-bottom: 10px;">
                @Html.TextBoxFor(x => Model.Acoes, new { @type = "checkbox", @id = item.SelectedValue, @value = item.SelectedValue})
                <label for="@item.SelectedValue">
                    <b>@item.Title</b>
                    <br />
                    <span class="perfil-filtro-expander-descricao">
                        @item.Description
                    </span>
                </label>
        </div>
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.