Get dynamically generated element id (BeginCollectionItem)

0

I have a View , where I get the selected value in a DropDownList , use that value in a ajax query, and populate another DropDownList with the query return. Until then, that's fine. However, these DropDownList's will be generated dynamically using the BeginCollectionItem , thereby generating a id different for each item generated. With this, I need to get this value, to send as a parameter in my queries.

In My view , I'm using the following code:

@using (Html.BeginCollectionItem("ClientesPrestacoes"))
{
    @Html.HiddenFor(model => model.Cliente.CidadeId)
     <div class="form-group col-md-9 col-lg-offset-1">
        @Html.LabelFor(model => model.Conjunto, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-2 editor-field">
            @Html.CheckBoxFor(model => model.Conjunto,  new { @class = "clickConjunto" })
            @Html.ValidationMessageFor(model => model.Conjunto, "", new { @class = "text-danger" })
        </div>
        <div id="divClienteConjuntoAdd">
            @Html.LabelFor(model => model.ClienteConjuntoId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4 editor-field">
                @Html.DropDownListFor(model => model.ClienteConjuntoId, new SelectList(""), new { @class = "form-control", @id = "ClienteConjuntoP" })
                @Html.ValidationMessageFor(model => model.ClienteConjuntoId, "", new { @class = "text-danger", @id = "ClienteConjuntoId" })
            </div>
        </div>
    </div>
        <input type="button" class="removebtn btn btn-danger col-md-1" value="X" id="removebtn" title="Excluir Pretação" style="width: 30px" />
    </div>
}

What is generated in html is this:

<div class="form-group col-md-9 col-lg-offset-1">
    <label class="control-label col-md-2" for="ClientesPrestacoes_ca186a13-315c-46e4-b7b3-b2dd3063ddb4__Conjunto">Cliente Conjunto?</label>
    <div class="col-md-2 editor-field">
        <input class="clickConjunto" data-val="true" data-val-required="O campo Cliente Conjunto? é obrigatório." id="ClientesPrestacoes_ca186a13-315c-46e4-b7b3-b2dd3063ddb4__Conjunto" name="ClientesPrestacoes[ca186a13-315c-46e4-b7b3-b2dd3063ddb4].Conjunto" type="checkbox" value="true"><input name="ClientesPrestacoes[ca186a13-315c-46e4-b7b3-b2dd3063ddb4].Conjunto" type="hidden" value="false">
        <span class="field-validation-valid text-danger" data-valmsg-for="ClientesPrestacoes[ca186a13-315c-46e4-b7b3-b2dd3063ddb4].Conjunto" data-valmsg-replace="true"></span>
    </div>
    <div id="divClienteConjuntoAdd">
        <label class="control-label col-md-2" for="ClientesPrestacoes_ca186a13-315c-46e4-b7b3-b2dd3063ddb4__ClienteConjuntoId">Nome Cliente</label>
        <div class="col-md-4 editor-field">
            <select class="form-control" data-val="true" data-val-number="The field Nome Cliente must be a number." id="ClienteConjuntoP" name="ClientesPrestacoes[ca186a13-315c-46e4-b7b3-b2dd3063ddb4].ClienteConjuntoId"></select>
            <span class="field-validation-valid text-danger" data-valmsg-for="ClientesPrestacoes[ca186a13-315c-46e4-b7b3-b2dd3063ddb4].ClienteConjuntoId" data-valmsg-replace="true" id="ClienteConjuntoId"></span>
        </div>
    </div>
</div>

Each new item, it generates a id different.

I need to retrieve id to use as a parameter in my query ajax .

<script type="text/javascript">
$('.clickConjunto').change(function () {
    var checkId = $(this).attr('id');
    console.log(@cidadeId);
    if (document.getElementById(checkId).checked == true) {
        document.getElementById('divClienteConjuntoAdd').style.display = 'block';
        $.getJSON(BASE_URL + 'Cliente/ObterClientePorCidade', { cidadeId: @cidadeId }, function (data) {

            $('#ClienteConjuntoIdP option').remove();
            console.log('teste');
            if (data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    $('#ClienteConjuntoIdP').append('<option value="' +
                        data[i].ClienteId + '"> ' +
                        data[i].NomeEntidade + '</option>');
                }
            } else {
                $('#ClienteConjuntoIdP option').remove();
                $('#ClienteConjuntoIdP').append('<option> Essa cidade não possui Prefeituras</option>');
            }

        });
    } else {
        document.getElementById('divClienteConjuntoAdd').style.display = 'none';
    }
});

The value of checkBox I recover by clicking it. I do this in this part:

$('.clickConjunto').change(function () {
        var checkId = $(this).attr('id');

The problem is to recover the id of the DorpDownList generated. This I can not use the event click() or change() to retrieve the value.

I tried to use something like:

 var id = $(@Html.DropDownListFor(model => model.ClienteConjuntoId, new SelectList(""))).attr('id');

But I get error.

    
asked by anonymous 14.07.2015 / 16:08

0 answers