I have a MVC5 application using C#, jQuery and Select2 v3.5.2, together with Bootstrap 2.3.2.
Now I am new to the select2, so I am having some trouble. For example, I want a text box with multiple items, as seen in the example of Tagging support.
Now, this shouldn't be hard, but the problem is that I need to update those values dynamically in a Bootstrap modal, using jQuery, after making an Ajax call to the server.
It sounds complex, but it is simple. When a user clicks a button to Edit an item, I show a Bootsrap modal, with the information of that item, after querying the server for the information on that item:
//Show Edit Package modal
$("a.btn.btn-default.editPackage").click(function () {
//ask the server for item information
$.ajax({
url: $(this).data('url'),
type: 'get',
cache: false,
success: function (result) {
//this input field belongs to the modal
$(input #SubCategoryId).attr('data-edit-values', '13 - Orange');
$('#myModal').show();
}
});
return false; //prevent browser defualt behavior
});
And here is the Modal (mostly boiler plate code):
<div class="modal fade" id="@Model.modalId" tabindex="-1" role="dialog" aria-labelledby="@Model.modalId" aria-hidden="true">
<div class="modal-dialog" style="overflow-y: auto; max-height: 80vh;">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">@Model.modalTitle</h4>
</div>
<div class="modal-body" data-val="@Model.packageId">
<form class="form-horizontal">
<div class="control-group">
<label class="control-label">Select the Function:</label>
<div class="controls">
<input class="select2s" data-edit-values="@Model.functionsString" data-o2f-placeholder="Select Employee Function..." data-o2f-url="@Url.Action("FunctionsMultiple", "Home")" data-val="false" id="SubCategoryId" name="SubCategoryId" multiple="multiple" type="text"/>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" id="@Model.saveButtonId" data-url="@Model.saveButtonUrl">@Model.saveButtonText</button>
</div>
</div>
</div>
</div>
The problem is that when I load the popup, the values on the field are not loaded.
What am I doing wrong in my javaScript?