asp.net - Dynamic link issue in an MVC 3 website -
i'm creating first asp.net mvc 3 website company's intranet. it's pretty cool, play audio recorded our phone system , saved in our db. that's working good, i'm having hard time figuring out how should simple. please forgive syntax errors have, rough draft.
i have table in index view /apps list appname's, , next each appname want display link view, text of link being count() of calldetails associated app.
i have 2 classes:
public class apps { public int appid { get; set; } public string appname { get; set; } } public class calldetail { public int id { get; set; } public int appid { get; set; } public byte[] firstname { get; set; } public byte[] lastname { get; set; } ....etc }
a context each:
public class appscontext : dbcontext { public dbset<apps> apps { get; set; } } public class callcontext : dbcontext { public dbset<calldetail> calldetails { get; set; } }
a controller method each:
// appscontroller private appscontext db = new appscontext(); public viewresult index() { return view(db.apps.tolist()); } // callcontroller method (from current attempt) public actionresult callcheck(int id) { bool? enabled = null; var appcalls = s in db.calldetails s.appid == id && s.enabled.equals(enabled) select s; string callnum = appcalls.count().tostring(); return view(callnum); }
it displays appname fine in portion of view below, , can create link view each associated calldetail fine. don't know how display info i'd calldetail controller since view's model apps , controller, appscontroller.
@model ienumerable<mymessageplayer.models.apps> ... @foreach (var item in model) { <tr> <td> @html.displayfor(modelitem => item.appname) </td> <td class="applink"> ... </td> </tr> }
i've tried many different methods, might have gotten work, seemed semantically un-mvc. figured i'd ask general "whats standard practice?" type of question.
the path going down end hitting database each app have in database. there way display information 1 hit database.
your context needs change this:
public class applicationcontext : dbcontext { public dbset<apps> apps { get; set; } public dbset<calldetail> calldetails { get; set; } }
you create view model object called appcallinfo has 3 properties:
public class appcallinfo { public int appid { get; set; } public string appname { get; set; } public int callcount { get; set; } }
in index action need this:
public viewresult index() { var model = in db.apps join c in db.calldetails on a.appid equals c.appid c.enabled == enabled group new { appname = a.appname, appid = a.appid } g select new appcallinfo { appname = g.key.appname, appid = g.key.appid, callcount = g.count() }; return view(model.tolist()); }
now have need each row in table in 1 object.
@model list<mymessageplayer.viewmodels.appcallinfo> ... @foreach (var item in model) { <tr> <td> @html.displayfor(modelitem => item.appname) </td> <td class="applink"> @html.actionlink(item.callcount, "viewcalls", "call", new { id = item.appid }, null) </td> </tr> }
using method avoids hitting database each app have in table.
Comments
Post a Comment