murday1983
murday1983

Reputation: 4026

JQuery to add list items

I have an autocomplete input field which is using JQuery. The following JQuery does return my list

$(function ()
{
    var list = list[<%=additional.Count%>];
    <% for (var i = 0; i < additional.Count; i++)
    { %>
        list[<%=i%>] = <%=additional[i].Text %>;
    <% } %>
    var availableTags = list;

    $("#Scheme_TextBox").autocomplete({
        source: availableTags,
        select: function (event, ui)
        {
            SelectedOrigoScheme();
        }
    });
});

The list is generated in code behind as shown below

List<AJBG.ServiceContracts.Messages.ThirdPartyAPIs.Origo.GetStaticCedingSchemes.CedingScheme> origoCedingSchemes = target.GetStaticCedingSchemes(request).cedingSchemes;
            List<SelectListItem> listItems = new List<SelectListItem>();
            foreach (AJBG.ServiceContracts.Messages.ThirdPartyAPIs.Origo.GetStaticCedingSchemes.CedingScheme origoCedingScheme in origoCedingSchemes)
            {
                listItems.Add(new SelectListItem() { Text = origoCedingScheme.schemeName, Value = origoCedingScheme.counterPartySchemeOrigoId.ToString(), Selected = false });
            }
            var selectItemList = listItems as List<SelectListItem>;

My view

<%=Html.TextBox("Scheme_TextBox", "", new { style = "width:98%;", PlaceHolder= "Type to search...", onblur="SelectedOrigoScheme(this);" })%>

But when I run my code I get the following JQuery error

.....\n\nSCRIPT1004: Expected ';'

Below is some of the results it returns: $(function () { var list = list[2103];

        list[0] = The **Self Invested Personal Pension**;

        list[1] = WEALTH **at Work Ltd**;

        list[2] = Deloitte **Pension Plan CIMP**;

        list[3] = The **Carey Pension Scheme**;

        list[4] = Sippdeal **e**-sipp;

        list[5] = AEGON;

        list[6] = AEGON;

        list[7] = Alliance **Trust**;

        list[8] = Alliance **Trust**;

        list[9] = Ashby **London SIPP**;

        list[10] = Ashcourt **Rowan**;

        list[11] = Aviva;

        list[12] = Aviva;

        list[13] = AXA **Wealth Personal Pension Plan**;

        list[14] = AXA **Winterthur**;

        list[15] = Bank **of** Ireland SIPP;

        list[16] = Barclays Stockbrokers SIPP;

        list[17] = Brewin Dolphin SIPP;

        list[18] = Capita SIPP;

        list[19] = City Trustees;

        list[20] = Clerical Medical SIPP;

        list[21] = Clerical Medical Stakeholder Plan;

        list[22] = Cofunds Pension Account;

        list[23] = Dentons SIPP C G Drennen;

        list[24] = European Wealth Management Scheme Stockmarket SIPP;

        list[25] = Fidelity SIPP;

        list[26] = Friends Life;

        list[27] = Friends Life (Provident);

NOTE: All bold wording are my what my list fails on.

I tried doing list[<%=i%>] = "<%=additional[i].Text %>" but this caused the following error

JavaScript runtime error: Unable to get property '2103' of undefined or null reference

The 2103 is how many result where returned in my list in my code behind. Its probably something easy but I've spent the last 3hrs trying to resolve it with various people helping and can not solve the issue.

Upvotes: 0

Views: 59

Answers (1)

user3572680
user3572680

Reputation:

list[0] = The **Self

This is text (literal) and should be wrapped in quotes within JavaScript. Either singles or doubles is acceptable.

list[0] = 'my message string (literal)'

To be honest is looks like you want to be using a javascript view model framework ideally. It looks to me like your doing it the hard way.

See angular, knockout backbone etc.

Upvotes: 2

Related Questions