I have a View with dropDowlist that contains data that comes from the database and right down I put a buttom that when I click it will attach the selected dropdownlist item to a list, I made a code here but it is not appending if I putting default values works, but the dropDowlist values do not.
javascript
$("#addPeca").click(function () {
var resultData = $("#ListaPecas option:selected").text();
$(document).ready(function () {
var myselect = $('<ul>');
$.each(resultData, function (index, key) {
myselect.append($('<li></li>').val(key).html(key));
});
$('#listPeca').append(myselect.html());
});
});
In my view
<div class="form-group">
@Html.LabelFor(model => model.ListaPecas, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.PecasId,Model.Pecas,"Pecas", new { @class = "form-control" } )
@Html.ValidationMessageFor(model => model.ListaPecas, "", new { @class = "text-danger" })
<button class="btn btn-info" type="button" id="addPeca">Adicionar</button>
</div>
</div>
List where the selected value will be attached
<ul id="listPeca"></ul>