I am failing with Knockout select list binding when using an object as a select list value. It works fine if I use a string, but I want to bind objects.
I have a Gift object and it has a Title, Price and Company. I have a select list of companies and each company has an Id and Name. The initial selection however is not the correct in the select list.
Please see fiddle: http://jsfiddle.net/mrfunnel/SaepM/
This is important to me when binding to MVC3 view models. Though I admit it may be because I am doing things the wrong way.
If I have the following model:
public class Company
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public class GiftModel
{
public Company Company { get; set; }
public string Title { get; set; }
public double Price { get; set; }
}
How do I select a Company that is bindable in my controller? Do I need to add a CompanyId property to the GiftModel and bind to that or write custom binder. Am I missing something fundamental here?
Thanks in advance.
You need to do a lot of stuff.
An CompanyId in your ViewModel is the only way to bind and make this observable.
You can not make a object observable only it´s values
your model