0

I am trying to utilize the live-search functionality exposed by the Bootstrap-Select plugin in an ASP.NET MVC application.

I have managed to get it to work, i.e the required drop-downs are searchable, but the only problem is I have placed the code to that in a script tag.

The drop-downs are part of a display template and if I use script tags it seems that the rest of the elements in the razor view cannot access the JavaScript functions required to load the correct data.

My question is two fold. Firstly, if I use script tags on a Partial View/Editor/Display template does it negate all other external .js files?

Secondly how do I get the Bootstrap-Select live-search functionality to work from an external .js file on a Partial View/Editor/Display template?

Below is what I used to get the drop-downs to be searchable:

@model EacWebApp.Models.ViewModels.Shared.NewPortfoliosViewModel
<script type="text/javascript" src="~/Scripts/bootstrap-select.js"></script>
<link rel="stylesheet" type="text/css" href="~/Content/bootstrap-select.min.css" />
<script>
    $(function() {
        $('.selectpicker').selectpicker({
            livesearch: true
        })
    });
</script>
<div class="row form-allocation-table-header-row">
    <div class="col-sm-1 form-row-label text-center">
        DISC
    </div>
    <div class="col-sm-5 form-row-label text-center">
        PORTFOLIO
    </div>
    <div class="col-sm-3 form-row-label text-center">
        ALLOCATION
    </div>
    <div class="col-sm-1 form-row-label text-center" style="padding: 0 5px;">
        TIC
    </div>
    <div class="col-sm-1 form-row-label text-center"></div>
</div>

@for (int i = 0; i < Model.MaxNumberOfAvailablePortfolios; i++)
{
    <div class="row portfolio-allocation-dropdown">
        <div class="selected-portfolio-data">
            <input type="hidden" data-property-source="data-portfolio-name" name="Portfolios[@i].Name" />
            <input type="hidden" data-property-source="value" name="Portfolios[@i].FundId" />
            <input type="hidden" data-property-source="data-portfolio-id" name="Portfolios[@i].Id" />
            <input type="hidden" data-property-source="data-portfolio-udw-id" name="Portfolios[@i].FundUdwId" />
            <input type="hidden" data-property-source="data-portfolio-class" name="Portfolios[@i].Class" />
            <input type="hidden" data-property-source="data-portfolio-discount" name="Portfolios[@i].IsDiscountable" />
            <input type="hidden" data-property-source="data-is-phase-in-allowed" name="Portfolios[@i].IsPhaseInAllowed" />
            <input type="hidden" data-property-source="data-is-phase-out-allowed" name="Portfolios[@i].IsPhaseOutAllowed" />
            <input type="hidden" data-property-source="data-tic-real-value" name="Portfolios[@i].TIC" value="tic" />
            <input type="hidden" data-property-source="portfolio-allocation" name="Portfolios[@i].AllocationPercent" />
        </div>
        <div class="col-sm-1">
            <input type="checkbox" onclick="return false;" class="is-discounted" />
        </div>
        <div class="col-sm-5">
            <select class="form-row-dropdown portfolioList selectpicker" id="portfolioDropDown" data-live-search="true" onchange="fillSelectedPortfolioData(this)">
                <option value="empty">Empty Slot</option>
                @{
                    foreach (var portfolio in Model.AvailablePortfolios)
                    {
                        <option value="@portfolio.FundId" data-is-phase-in-allowed="@portfolio.IsPhaseInAllowed" data-is-phase-out-allowed="@portfolio.IsPhaseOutAllowed"
                                data-portfolio-discount="@portfolio.IsDiscountable" data-portfolio-udw-id="@portfolio.FundUdwId" data-portfolio-id="@portfolio.Id"
                                data-portfolio-class="@portfolio.Class" data-portfolio-name="@portfolio.Name">
                            @portfolio.Name
                        </option>
                    }
                }
            </select>
        </div>
        <div class="col-sm-3 allocation-inputs collapsed">
            <button class="minus-button-holder">
                <span class="glyphicon glyphicon-minus"></span>
            </button>
            <input class="text-center portfolio-allocation-percentage-input pseudo-verticaly-centered" value="0" oninput="validateNumberInput(event, this)" onfocus="removeZeroFromInput(this)" onblur="addZeroToInput(this)" /><span class="percentage-sign pseudo-verticaly-centered">%</span>
            <button class="plus-button-holder">
                <span class="glyphicon glyphicon-plus"></span>
            </button>
        </div>
        <div class="col-sm-3 no-portfolio-selected" style="text-align:center">-</div>
        <div class="col-sm-1 no-portfolio-selected" style="text-align:center">-</div>
        <div class="col-sm-1 portfolio-tic-container collapsed" style="padding: 0">
            <input type="text" class="portfolio-tic pseudo-verticaly-centered" data-tic-real-value="0" readonly="readonly" oninput="changeTicValue(event, this)" onfocus="removeZeroFromInput(this)" onblur="addZeroToInput(this)" /><span style="display: inline-block;" class="pseudo-verticaly-centered">%</span>
        </div>
        <div class="col-sm-1 text-center">
            <button class="portfolio-allocation-remove-allocation-percentage">
                <span class="glyphicon glyphicon-remove"></span>
            </button>
        </div>
    </div>
}

The drop-downs work fine, but the other .js functionality is seems to be negated.

LuTheZy
  • 61
  • 8
  • move all scripts from display/edit template to the main view. – LazZiya Nov 29 '18 at 08:34
  • There is a partial view which calls this display template, is that what you mean by the main view? – LuTheZy Nov 29 '18 at 09:11
  • the view that is returned by return View() call – LazZiya Nov 29 '18 at 09:19
  • I've placed the script in the Index view, which in this case is the main view, but the script seems to run before the Display Template is rendered. Is there a way to get the script to run after the display template is rendered? – LuTheZy Nov 29 '18 at 10:12
  • If I add the selectpicker class to the select element then the drop-down does not display but when I remove this class, it displays fine. – LuTheZy Nov 29 '18 at 10:18
  • have you checked this reply https://stackoverflow.com/a/40757646/5519026 – LazZiya Nov 30 '18 at 05:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184506/discussion-between-luthezy-and-laz-ziya). – LuTheZy Nov 30 '18 at 06:13
  • Did u figure this out im having same issue the dropdown will not drop down – c-sharp-and-swiftui-devni Jul 06 '20 at 17:35

0 Answers0