javascriptjqueryasp.netasp.net-mvcjquery-select2

Select2 -- placeholder not displaying


I'm using Select2 Plugin in my asp.net mvc 5 application. according to the documentation

The placeholder option allows you to pass in a data object instead of just a string if you need more flexibility. The id of the data object should match the value of the placeholder option.

I have done exactly that, but the placeholder is still not showing up.

Code:

model.Step6.Titles.Insert(0, new SelectListItem() { Text = "Select a Title", Value = "0", Selected = true });

@Html.DropDownListFor(model => model.Step6.Title, Model.Step6.Titles, new { id="teamtitle", @style = "width: 100%;"})

$("select").select2({
    allowClear: true,
    placeholder: {
        id: "0",
        placeholder: "Select an Title"
    }
})

can someone show me what I'm doing wrong here?


Solution

  • I think placeholder is a string. Not an object https://select2.github.io/examples.html#placeholders

    $("select").select2({
        allowClear: true,
        placeholder:"Select an Title"
    
    })