开发者

using @data-bind in ASP.NET MVC htmlAttributes throws exception

I am using ASP.NET MVC 3.0 and KnockoutJS. I was trying to add the binding into my View Helpers like this ...

@Html.TextBoxFor(model => model.Name, new { @placeholder = "Name", @size = "35", @data-bind = "value: aName" })

开发者_StackOverflowBut this throws the exception ...

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

Can anyone enlighten me on what this means - and moreover, how to solve it?


It doesn't like the hyphen in the property name. To fix this change @data-bind to @data_bind and this will then render with a hyphen on the page.


If you are using MVC2, you can use the following jquery function to change the data_bind to data-bind:

        // MVC2 fix for data_bind
        $('[data_bind]').each(function(i, item) {
            item = $(item);
            item.attr("data-bind",item.attr("data_bind"));
            item.removeAttr("data_bind");
        });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜