Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Knockout's data-bind attribute in Mvc helpers like Html.EditorFor()

I tried this @Html.EditorFor(model => model.Name, " ", new { data_bind = "value:firstName" }); and other possible overloades but none of them seem to work.

The rest of code:

<script type="text/javascript">
$(document).ready(function () {

    function AppViewModel() {
        this.firstName = ko.observable("");
        this.lastName = ko.observable("");
    }
    ko.applyBindings(new AppViewModel());
});

like image 913
Vlado Pandžić Avatar asked May 23 '13 17:05

Vlado Pandžić


1 Answers

Your 3rd parameter to EditorFor does not do what you think it should be doing.

See http://msdn.microsoft.com/en-us/library/ff406461(v=vs.98).aspx

EditorFor cannot add HTML attributes to the element. Use TextBoxFor instead:

@Html.TextBoxFor(model => model.Name, new { data_bind = "value:firstName" });
like image 79
Matt Houser Avatar answered Sep 20 '22 13:09

Matt Houser