Skip to main content
Home Forums Silverlight Programming WCF RIA Services how to get data from the server
6 replies. Latest Post by samw on July 8, 2009.
(0)
samw
Member
67 points
124 Posts
07-03-2009 2:05 AM |
How do I effect a transfer of data from the server to the client using the following code? Do I need to write a method on the server side?
Note that the class FeedbackView does not exist in my DataModel.
public FeedbackPage() { InitializeComponent(); PersonalWebsiteDomainContext context = new PersonalWebsiteDomainContext(); this.DataContext = context; List query = (from f in context.Feedbacks join a in context.Articles on f.ArticleID equals a.ArticleID select new FeedbackView { FeedbackID = f.FeedbackID, ParentID = f.ParentID, ArticleID = f.ArticleID, ArticleName = a.Title, Date = f.Date, SenderName = f.SenderName, SenderIPAddress = f.SenderIPAddress, Comment = f.Comment, Approved = f.Approved }).ToList(); dg.ItemsSource = query; } protected class FeedbackView { public int FeedbackID {get; set;} [Key] public int ParentID {get; set;} public int ArticleID {get; set;} public string ArticleName {get; set;} public DateTime Date {get; set;} public string SenderName {get; set;} public string SenderIPAddress {get; set;} public string Comment {get; set;} public bool Approved { get; set; } }
codenenterp
36 points
13 Posts
07-04-2009 12:34 AM |
you will have to write some type of service for the silverlight client to call and get the data. The call to the server is async so dont be suprised.
theo67
524 points
177 Posts
07-04-2009 2:52 AM |
You need to call a method on the context before you create "query" else "context.Feedback" will be empty. Normally you could create a LINQ-query and pass it to the DomainContext. Another way is to implement a specific query method in the DomainService which will be exposed via the DomainContext and cal be called in your client. See the QueryAttribute. Anyway when you use the DomainDataSource you can declare this in XAML.
Have you considered using a viewmodel that wraps the PersonalWebsiteDomainContext, like http://stackoverflow.com/questions/744474/combining-net-ria-services-and-mvvm-in-silverlight-3-0. This way you move the query in the viewmodel.
ippon11
24 points
7 Posts
07-06-2009 12:28 PM |
Alternatively Sam, for a quick mock-up (unsecure), you can create xml as your data, and either embed it as a resource in your SL project, or use a webclient to retrieve it from a remote location via http. You can then use your existing code to query the xml as an xmldatasource. This not a productised solution but will get you up and running quickly to prototype the rest of your SL app.
Michael
...Mark this as answer if it has been helpful.
07-08-2009 1:26 AM |
Thanks all for the replies. I was on vacation this weekend. OK here is what I did. It mostly works but I'm not sure it's the best way. The problem I'm having is using the IQueryable object. See commented section of code where I set the ItemsSource of the datagrid.1.) Server: Made a file for my "...View" classes:
namespace PersonalWebsite1.Web{ public class FeedbackView { [Key] public int FeedbackID { get; set; } public int ParentID { get; set; } public int ArticleID { get; set; } public string ArticleName { get; set; } public DateTime Date { get; set; } public string SenderName { get; set; } public string SenderIPAddress { get; set; } public string Comment { get; set; } public bool Approved { get; set; } }}
namespace PersonalWebsite1.Web{ public partial class PersonalWebsiteDomainService : LinqToEntitiesDomainService { public IQueryable GetFeedbackView() { var query = (from f in Context.Feedback join a in Context.Articles on f.ArticleID equals a.ArticleID select new FeedbackView { FeedbackID = f.FeedbackID, ParentID = f.ParentID, ArticleID = f.ArticleID, ArticleName = a.Title, Date = f.Date, SenderName = f.SenderName, SenderIPAddress = f.SenderIPAddress, Comment = f.Comment, Approved = f.Approved }).AsQueryable(); return query; } }}
3) Client: Set the ItemsSource of the datagrid as follows:
namespace PersonalWebsiteAdmin.Views{ public partial class FeedbackPage : BasePage { public FeedbackPage() { InitializeComponent(); PersonalWebsiteDomainContext Context = new PersonalWebsiteDomainContext(); //Does not work. IQueryable query = Context.FeedbackViews.AsQueryable().Where(f => f.FeedbackID < 7); Context.LoadFeedbackView(query,null); dg.ItemsSource = query; // Works //Context.LoadFeedback(); //dg.ItemsSource = Context.Feedbacks; }
07-08-2009 3:24 AM |
Have you tried binding to the Context.Feedbacks?
Context.Loaded += new EventHandler< System.Windows.Ria.Data.LoadedDataEventArgs >(OnDataLoaded); IQueryable query = Context.FeedbackViews.AsQueryable().Where(f => f.FeedbackID < 7); Context.LoadFeedbackView(query,null); ... void OnDataLoaded(object sender, System.Alexandria.Data.Client.DataLoadedEventArgs e) { Context.Loaded -= new EventHandler< System.Windows.Ria.Data.LoadedDataEventArgs >(OnDataLoaded); dg.ItemsSource = Context.Feedbacks; }
This LoadFeedbackView() will result in an async request, so the Context.Feedbacks won't be populated immediately.
07-08-2009 1:32 PM |
Excellent! Thank you Theo.