Insert checkbox dynamically by Json return does not display Text?

4

I am creating a list of Checkbox according to the selection of a Select on the same screen. In the event change of select is called via ajax javascript , the method that returns a Json with list (value, text) and scrolling through this list I would dynamically create my% with%.

Problem: The Checkbox appears, but there is no Text on the screen. although% generated% is right in my eyes.

Code:

Method that returns Json:

SelectList lista = new SelectList(string.Empty, "SearchParameterId", "Name");
if (specificationId > 0)
{
   lista = new SelectList(db.SearchParameters
          .Include(s => s.Specification)
          .Where(sp => sp.Specification.SpecificationId == specificationId
             && sp.isActive == 1).AsEnumerable(), "SearchParameterId", "Name");
}
return Json(lista);

Checkbox of HTML :

$("#SpecificationId").change(function () {
        $("#ParamsCheckboxes").empty();
        $.ajax({
            type: 'POST',
            url: '/DropdownCascade/GetSearchParametersBySpecification', 
            dataType: 'json',
            data: { id: $("#SpecificationId").val() },
            success: function (categories) {
                $.each(categories, function (i, category) {             

                    alert(category.Text);
                    alert(i);

                    var br = document.createElement('br');                      

                    var input = document.createElement('input');
                    input.type = 'checkbox';
                    input.name = category.Text;
                    input.id = category.Value;
                    input.value = category.Text;
                    input.textContent = category.Text;

                    document.getElementById("ParamsCheckboxes").appendChild(input);
                    document.getElementById("ParamsCheckboxes").appendChild(br);

                });
            },
            error: function (ex) {
                alert('Falha ao carregar as categorias N3.' + ex);
            }
        });
        return false;
    });

Finally the dropdownlist and then the div that I try to create the checkboxes in:

<div class="form-group">
    @Html.LabelFor(model => model.Specification, 
          htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-6">
        @Html.DropDownList("SpecificationId", 
             new SelectList(string.Empty, "Value", "Text"), 
               "Selecione a Sub-Categoria", 
               htmlAttributes: new { @id = "SpecificationId", @class = "form-control" })
    </div>
</div>
<!-- START CHECKBOXES-->
<div class="form-group">
    <div class="col-md-2">
        Detalhes Técnicos:      
        <div id="ParamsCheckboxes">

        </div>      
    </div>
</div>
<!-- END CHECKBOXES-->

It returns the data to the screen, I put a Javascript before View with the category.Text and it shows all the values, it just does not create the alert .

Example of% generated%:

<div id="ParamsCheckboxes">
   <input type="checkbox" name="1tb" id="2" value="1tb" style="color: black;"></input>
   <br>
   <input type="checkbox" name="5tb" id="3" value="5tb"></input>
   <br>
   <input type="checkbox" name="20tb" id="4" value="20tb"></input>
  <br>
</div>

var checkBox = document.createElement('checkbox') was on my own, but that's it. Any ideas ?

    
asked by anonymous 14.01.2017 / 22:09

2 answers

2

What you are developing is not the best solution, the best solution, and make Framework responsible for generating this html ( select1 ) instead of _checkBox . In% of% of% with% was used $ .post and set its return to . Ready with these changes the list is dynamically assembled if issues make it easier to change, adding , behavior, etc.

15.01.2017 / 02:12
2

Here is a code that I have managed that can help you, generating Checkbox dynamically in MVC using Jquery, I hope it helps.

$(function(){



        $('#Categoria').change(function(){


                $.ajax({
                    url: '@Url.RouteUrl(new{ action="GetSubCategoria", controller="Home"})',
                    data: {"Categoria": $('#Categoria').val()},
                        type: 'GET',
                        dataType: 'json',
                        success: function(resp) {
                        $.each(resp,function(key,value)
                        {
                           var newChk = "<input type='checkbox' name='SubCategoria' value='"+resp[key].SubCategoriaID+"' />" +resp[key].Nome + "<br />";
                           $("#ParamsCheckboxes").append(newChk);

                        });

                }, error:function(index, e, error){
                      alert(error);
    }});

        });

    });

Follow the complete dotnet fiddle example.

link

    
15.01.2017 / 01:36