dont see an empty text box inside the drop down

Anjali Agarwal 1,426 Reputation points
2023-04-27T04:33:47.96+00:00

I have the following code on my web page. I am trying to use select2 so that user can type in the value inside the drop down.

<div class="card card-primary">

<div class="row">

    <div class="card-body">

        <div class="col-md-12">

            <form asp-action="Create">

              @*  <div asp-validation-summary="ModelOnly" class="text-danger"></div>*@

                <div class="form-group row">

                    <div class="col-sm-4">

                     

                        <select id="ddlType" name="Name" class="form-control" asp-items="ViewBag.Select">

                            <option value="0">--Select Type--</option>

                        </select>

                    </div>

                </div>

                <div class="form-group">

                    <input type="submit" value="Create" class="btn btn-primary" />

                </div>

            </form>

        </div>

    </div>

</div>

</div>

link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/css/select2.min.css" />

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/js/select2.min.js"></script>

<script type="text/javascript">

$(function () {

    debugger;

    $("#ddlType").select2({

      

        tags: true,

        createTag: function (params) {

            return {

                id: params.term,

                text: params.term,

                newOption: true

            }

        },

        templateResult: function (data) {

            var $result = $("<span></span>");

            $result.text(data.text);

            if (data.newOption) {

                $result.append(" <em>(new)</em>");

            }

            return $result;

        }

    });

});

</script>

I don't have the script section because I have this in my layout page

<script src="~/lib/jquery/dist/jquery.min.js"></script>

<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>

<script src="~/js/site.js" asp-append-version="true"></script>

await RenderSectionAsync("Scripts", required: false)

This is what I have in the controller.

 public async Task<IActionResult> Create()

        {

           List<DocumentType> GetAllDocType =  await _documentTypeService.GetAllDocTypes();

		   SelectList Typo = new SelectList(GetAllDocType.DistinctBy(x=>x.Type), "Id", "Type");

           ViewBag.Select = Typo;

           return View();

        }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,514 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,454 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 64,161 Reputation points
    2023-04-27T15:26:03.3666667+00:00

    you should use the browser debug tools you would see the error. view page source and be sure the script includes are before the inline javascript.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.