c# - Empty client-side models -
i'm working on windows phone 8.1 app, , i'm having trouble getting client's model objects obtain values database i'm hosting on azure. code i've written seems work (at least iterate correctly), problem bars objects empty. don't know how them obtain values stored in db (barid, barname, etc). i've built working services project i've published azure website. project contains dtos, context, , controllers. client project contains models , views. need change architecture? if so, how should change it? here's code that's being called in controller:
public class barscontroller : apicontroller { private gatoradeshowerdb db = new gatoradeshowerdb(); // get: api/bars // [route("http://gatoradeshower.azurewebsites.net/api/bars/all")] public iqueryable<bar> getbars() { return db.bars; } // get: api/bars/5 [responsetype(typeof(bar))] public async task<ihttpactionresult> getbar(int id) { bar bar = await db.bars.findasync(id); if (bar == null) { return notfound(); } // return ok(bar); return ok(bar); } // put: api/bars/5 [responsetype(typeof(void))] public async task<ihttpactionresult> putbar(int id, bar bar) { if (!modelstate.isvalid) { return badrequest(modelstate); } if (id != bar.barid) { return badrequest(); } db.entry(bar).state = entitystate.modified; try { await db.savechangesasync(); } catch (dbupdateconcurrencyexception) { if (!barexists(id)) { return notfound(); } else { throw; } } return statuscode(httpstatuscode.nocontent); } // post: api/bars [responsetype(typeof(bar))] public async task<ihttpactionresult> postbar(bar bar) { if (!modelstate.isvalid) { return badrequest(modelstate); } db.bars.add(bar); await db.savechangesasync(); return createdatroute("defaultapi", new { id = bar.barid }, bar); } // delete: api/bars/5 [responsetype(typeof(bar))] public async task<ihttpactionresult> deletebar(int id) { bar bar = await db.bars.findasync(id); if (bar == null) { return notfound(); } db.bars.remove(bar); await db.savechangesasync(); return ok(bar); } protected override void dispose(bool disposing) { if (disposing) { db.dispose(); } base.dispose(disposing); } private bool barexists(int id) { return db.bars.count(e => e.barid == id) > 0; } }
here's dto code:
public partial class bar { public int barid { get; set; } [required] [stringlength(255)] public string barname { get; set; } [required] [stringlength(255)] public string barstreet { get; set; } [required] [stringlength(50)] public string barcity { get; set; } [required] [stringlength(50)] public string barstate { get; set; } [required] [stringlength(50)] public string barphone { get; set; } [required] [stringlength(50)] public string barzip { get; set; } }
here's context code:
public partial class gatoradeshowerdb : dbcontext { public gatoradeshowerdb() : base("name=gatoradeshowerdb") { } public virtual dbset<bar> bars { get; set; } public virtual dbset<event> events { get; set; } public virtual dbset<game> games { get; set; } public virtual dbset<group> groups { get; set; } public virtual dbset<member> members { get; set; } public virtual dbset<stadium> stadia { get; set; } public virtual dbset<team> teams { get; set; } public virtual dbset<teamstest> teamstests { get; set; } public virtual dbset<watchparty> watchparties { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<game>() .property(e => e.gametime) .isfixedlength(); modelbuilder.entity<teamstest>() .property(e => e.teamname) .isunicode(false); } }
here's code xaml.cs file (view):
public async void getbars() { using (var client = new httpclient()) { barslistbox.items.add("using block entered"); client.baseaddress = new uri("http://nflff.azurewebsites.net"); client.defaultrequestheaders.accept.clear(); client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); barslistbox.items.add("client's defaultrequestheaders done"); httpresponsemessage response = await client.getasync("api/bars"); barslistbox.items.add("httpresponsemessage object created getasync"); barslistbox.items.add(response.content.tostring()); if (response.issuccessstatuscode) { barslistbox.items.add("if entered"); ilist<bars> bars = await response.content.readasasync<ilist<bars>>(); //successfully retrieves bars items need format printing //wait... how can instantiate list ... read itself? (am wrong) nothing being stored? //barslistbox.items.add(bars); //this prints system.collection.generic... barslistbox.items.add(bars.count); //list contains correct number of items (int = 0; < bars.count; i++) { barslistbox.items.add("for entered"); //iterating correct number of times barslistbox.items.add(i); //when printing bar[i], prints empty object } //foreach (var bar in bars) //{ // barslistbox.items.add("foreach entered"); //not entering foreach // barslistbox.items.add(bar.tostring()); //} barslistbox.items.add("after foreach loop"); //getting printed } barslistbox.items.add("after if"); } }
and here's (client-side) model's code:
class bars { private int barid { get; set; } private string barname { get; set; } private string barstreet { get; set; } private string barcity { get; set; } private string barstate { get; set; } private string barphone { get; set; } private string barzip { get; set; } //public bars() //{ // //empty constructor //} public override string tostring() { return "bar: " + barid.tostring() +" " + barname.tostring() + " phone: " + barphone; //model obj not obtaining values } }
i'm @ complete loss first time creating mobile app hosted database. @ moment, i'm getting correct number of bars items returned readasasync call in view, they're empty. how can these bars objects contain proper values? database sql database .net back-end hosted on azure web sites.
i figured out problem. code working correctly. problem when deployed database azure website, created new database same tables , formats without data. when ran web api through localhost, worked fine because using database had sample data in it, hosted database didn't nothing show up. fix this, imported sample data new hosted database , worked correctly.
Comments
Post a Comment