I am doing one project in MVC using EF and binding using Knockout js.
If i will work with single table it is working correctly..
<b><u>My Class File is</u> </b>
<pre lang="cs">namespace PTGMessenger
{
using System;
using System.Collections.Generic;
public partial class tbluser
{
public string userid { get; set; }
public string screen_name { get; set; }
public string wants_notification { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string job_title { get; set; }
public string email { get; set; }
public string pwsd { get; set; }
public Nullable<long> phone { get; set; }
}
}</pre>
This is my controller code
public ActionResult Index()
{
ViewBag.userid = Session["userid"];
ViewBag.jobtitle = Session["jobTitle"];
return View();
}
[HttpGet]
public JsonResult GetByUserId(string Id)
{
using (PTGMessengerEntities4 entities = new PTGMessengerEntities4())
{
Id = Session["userid"].ToString();
var item = entities.tblusers.Single(x => x.userid == Id);
return Json(item, JsonRequestBehavior.AllowGet);
//return entities.tblusers.SingleOrDefault(x => x.userid == "T108");
}
}
and in View I am binding like this
var self = this;
self.screen_name = ko.observable("");
self.wants_notification = ko.observable("");
self.first_name = ko.observable("");
self.last_name = ko.observable("");
self.job_title = ko.observable("");
self.email = ko.observable("");
self.phone = ko.observable("");
var Employee = {
screen_name: self.screen_name,
wants_notification: self.wants_notification,
first_name: self.first_name,
last_name: self.last_name,
job_title: self.job_title,
email: self.email,
phone: self.phone
};
self.Employee = ko.observable();
self.Employees = ko.observableArray();
$.ajax({
url: '@Url.Action("GetByUserId", "ProfilePage")',
type: 'GET',
contentType: 'application/json; charset=utf-8',
datatype:'json',
data: {},
success: function (data) {
self.Employees(data);
},
error: function () {
alert("error");
}
});
}
$(document).ready(function () {
ko.applyBindings(new EmployeeModel());
});
My binding Data
<tr><td data-bind="text: screen_name"></td></tr>
<tr> <td data-bind="text: first_name"></td></tr>
<tr> <td data-bind="text: last_name"></td></tr>
<tr><td data-bind="text: job_title"></td></tr>
This one is perfectly executed... but instead of using one table if i make it nested means if i used multiple table with foreign key and primary key...... it is getting error during binding....
My model....
namespace PTGMessenger
{
using System;
using System.Collections.Generic;
public partial class tbluser
{
public tbluser()
{
this.contact_list = new HashSet<contact_list>();
this.contact_list1 = new HashSet<contact_list>();
this.conversation_participants = new HashSet<conversation_participants>();
this.messages = new HashSet<message>();
this.tblGroupPages = new HashSet<tblGroupPage>();
}
public string userid { get; set; }
public string screen_name { get; set; }
public string wants_notification { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string job_title { get; set; }
public string email { get; set; }
public string pwsd { get; set; }
public Nullable<long> phone { get; set; }
public virtual ICollection<contact_list> contact_list { get; set; }
public virtual ICollection<contact_list> contact_list1 { get; set; }
public virtual ICollection<conversation_participants> conversation_participants { get; set; }
public virtual ICollection<message> messages { get; set; }
public virtual ICollection<tblGroupPage> tblGroupPages { get; set; }
}
}
and rest code for controller and view is same nothing changes...
but no output this time.
If any one having some idea please tell me...