I was looking at the Contacts editor sample on the knockout.js website:
http://knockoutjs.com/examples/contactsEditor.html
The sample works perfectly, but I needed to make two changes to it:
Classes
public class Phone
{
public string Type { get; set; }
public string Number { get; set; }
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Phone> Phones { get; set; }
}
Sample Controller side code
List<Phone> phoneList = new List<Phone>();
Person p1 = new Person()
{
FirstName = "Abc",
LastName = "Xyz"
};
Phone phone1 = new Phone()
{
Type = "Home",
Number = "1111111111"
};
Phone phone2 = new Phone()
{
Type = "Mobile",
Number = "1111111112"
};
phoneList.Add(phone1);
phoneList.Add(phone2);
p1.Phones = phoneList;
List<Phone> phoneList2 = new List<Phone>();
Person p2 = new Person()
{
FirstName = "Pqr",
LastName = "Stu"
};
Phone phone3 = new Phone()
{
Type = "Home",
Number = "1111111113"
};
Phone phone4 = new Phone()
{
Type = "Mobile",
Number = "1111111114"
};
phoneList2.Add(phone3);
phoneList2.Add(phone4);
p2.Phones = phoneList2;
people.Add(p1);
people.Add(p2);
ViewBag.InitialData = Json(people, JsonRequestBehavior.AllowGet);
I would really appreciate if someone can help me with this.
Create a new project in ASP.NET MVC 4 and name it as you prefer and select empty project template. Install Entity Framework 6, Jquery and Knockout in your project using NuGet Package Manager. You can also download jquery. js and Knockout.
The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.
A ViewModel can be any type of JavaScript variable. In Example 1-3, let's start with a simple JavaScript structure that contains a single property called name .
Yes, knockout. js does apply the MVVM pattern. It's explained in the documentation: A model: your application's stored data.
The Json
method you are calling in your controller is meant to return a JsonResult
it does not create a JSON string. You would use this method to return json from an ajax call.
To return a JSON string to a view use something like this.
JavaScriptSerializer serializer = new JavaScriptSerializer();
ViewBag.InitialData = serializer.Serialize(people);
Then in your view code
<script>
var initialData = '@Html.Raw(ViewBag.InitialData)';
....
</script>
To answer your second question. In order to pass global list data such as this simply define a new class ContactsList
e.g
public class ContactsList
{
public string Name { get;set; }
public string Owner { get;set; }
public IList<People> People { get;set; }
}
Populate this and pass this to the JavascriptSerializer
instead. You will obviously need to adjust your js ContactsModel
accordingly.
EDIT
Here's a jsfiddle that demonstrates the changes needed.
http://jsfiddle.net/madcapnmckay/jRjwU/
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With