Adam Levitt
Adam Levitt

Reputation: 10476

knockout.js ASP.NET data-bind to dropdownlist (Visual Studio 2008)

I'm attempting to data-bind a value to an ASP.NET DropDownList but the server complains that it doesn't recognize the data-bind attribute:

<%=Html.DropDownList("accountSiteInstanceId", ViewData["degreePrograms"] as SelectList, new { @data-bind = "value: DegreeProgramId" } ) %>

I get the following error: Compiler Error Message: CS0746: Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

Upvotes: 1

Views: 2138

Answers (2)

Yannick Smits
Yannick Smits

Reputation: 875

change the dash into an underscore and it will convert it back to dash on runtime.

Upvotes: 4

Marian Ban
Marian Ban

Reputation: 8168

The problem is the dash. Use dictionary based notation:

<%= Html.DropDownList("accountSiteInstanceId", ViewData["degreePrograms"] as SelectList, new Dictionary<string, object>
                {
                        {"data-bind", 
                        "DegreeProgramId"}
                }) %>

Upvotes: 5

Related Questions