Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout data-bind attribute in razor syntax

I have Html written in Razor syntax:

@for (var i = 0; i < Model.AllBetStatuses.Count; ++i)
{
    <li class="betReportingCheckbox">
        @Html.CheckBoxFor(m => m.AllBetStatuses[i].Checked, new { @class =   
         "betStatusCheckboxes"})
        @Html.DisplayFor(m => m.AllBetStatuses[i].Name)
        @Html.HiddenFor(m => m.AllBetStatuses[i].Value)
    </li>
}

I want to use knockout.js to bind these values, but when I try something of this type:

@Html.CheckBoxFor(m => m.AllBetStatuses[i].Checked, new { @class =   
         "betStatusCheckboxes", @data-bind="..."})

I get a syntax error, because the '-' character is not valid there. Is there any simple way to do this using Razor syntax?

like image 400
Fat Shogun Avatar asked Oct 02 '13 11:10

Fat Shogun


People also ask

What is data bind knockout?

Knockout's declarative binding system provides a concise and powerful way to link data to the UI. It's generally easy and obvious to bind to simple data properties or to use a single binding.

How do you bind data?

In a data binding process, each data change is reflected automatically by the elements that are bound to the data. The term data binding is also used in cases where an outer representation of data in an element changes, and the underlying data is automatically updated to reflect this change.

What is Razor syntax in MVC?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML.


1 Answers

Replace the '-' by an '_'

@Html.CheckBoxFor(m => m.AllBetStatuses[i].Checked, new { @class =   
     "betStatusCheckboxes", @data_bind="..."})

I hope it helps.

like image 87
Damien Avatar answered Nov 15 '22 07:11

Damien