Model field names different in Javascript view model

50 views
Skip to first unread message

John Tsombakos

unread,
Jul 1, 2015, 1:36:02 PM7/1/15
to knocko...@googlegroups.com
Very familiar with Knockout, and am trying KnockoutMVC for a new project.

However I'm seeing an issue with the names of my fields in my C# model and the Javascript / Knockout view model, such that the Ko bindings aren't working.

My model has fields such as:

   string FirstName
   string LastName

etc,

but the Javascript view model is showing as

first_name
last_name

So my binding in the cshtml file of: <td @custs.Bind.Text(m => m.FirstName)></td>
turns into: <td data-bind="text : $data.FirstName"></td>

but the ViewModel is:

var viewModelJs = {"Customers":[{"id":44,"password":null,"company":"","first_name":"First","last_name":"Test",  (more here)
ko.applyBindings(viewModelJs);

Help? Would like to use KnockoutMVC, but this is a deal breaker...

Thanks.

John Tsombakos

unread,
Jul 1, 2015, 3:24:19 PM7/1/15
to knocko...@googlegroups.com
Ugh. Nevermind.

My model object was coming from another assembly, and I didn't know it has some JSON.NET attributes on it:

        [JsonProperty("first_name")]
        public virtual string FirstName { get; set; }

Grr.. Now I can't just use the object directly. Oh well.
Reply all
Reply to author
Forward
0 new messages