I'm currently testing Select2.js in my ASP.NET MVC project.
The first test on a "normal" view just worked fine but now I'm trying to add this to a select box inside a partial view that gets loaded on Ajax Call and then displayed inside a <div>
on the normal view page.
The code to generate the dropdown looks like this:
@Html.DropDownList("addRole",
new SelectList(ViewBag.availableRoles, "Id", "Name"),
"Rolle auswählen",
new { @name="addRole", @class = "form-control" }
)
And on the end of the partial view file I added the following:
$(document).ready(function () {
//var data = [{ id: 1, text: "Test" }];
$("#addRole").select2({
//data: data
});
});
It looks like it works because of the look of the select box changes but when I try to open it, it just shows nothing. Same happens when I uncomment the data
variable code above.
It just doesn't show anything to me and I don't know why.
I've already tried to add the JavaScript code to $(document).ajaxComplete
or in the success
function from the AJAX call but it doesn't change anything.
ViewBag.availableRoles
contains valid values with fieldsId
andName
? – Tetterselect2
since it's not working without it too... – Bagwig$("#addRole").select2()
it works fine. – Psychic