c# - Need help counting records for MVC Web App -
c# - Need help counting records for MVC Web App -
i'm learning how stray code visual studio produces (mvc entity framework). have view displays contents of contact table. have page want count records. (obviously on simplification). have model , controller "contact" table. have model , controller "stats" getting sorts of mixed results various tutorials , questions answered. can help walk me through how can populate count on "stats" page? i'll honest, i've been working @ week , having hard time declaring variables.
here's contacts model:
using system.componentmodel.dataannotations; using system.globalization; namespace basiccontacts.models { public class contact { public int contactid { get; set; } [datatype(datatype.date)] [display(name = "intake date")] public string dateintake { get; set; } [display(name = "first name")] public string namefirst { get; set; } } } here's contacts controller:
using system; using system.collections.generic; using system.data; using system.data.entity; using system.linq; using system.net; using system.web; using system.web.mvc; using basiccontacts.models; namespace basiccontacts.controllers { public class ctcontroller : controller { private applicationdbcontext db = new applicationdbcontext(); // get: /ct/ public actionresult index() { homecoming view(db.contacts.tolist()); } // get: /ct/details/5 public actionresult details(int? id) { if (id == null) { homecoming new httpstatuscoderesult(httpstatuscode.badrequest); } contact contact = db.contacts.find(id); if (contact == null) { homecoming httpnotfound(); } homecoming view(contact); } // get: /ct/create public actionresult create() { homecoming view(); } // post: /ct/create // protect overposting attacks, please enable specific properties want bind to, // more details see http://go.microsoft.com/fwlink/?linkid=317598. [httppost] [validateantiforgerytoken] public actionresult create([bind(include="contactid,dateintake,namefirst")] contact contact) { if (modelstate.isvalid) { db.contacts.add(contact); db.savechanges(); homecoming redirecttoaction("index"); } homecoming view(contact); } // get: /ct/edit/5 public actionresult edit(int? id) { if (id == null) { homecoming new httpstatuscoderesult(httpstatuscode.badrequest); } contact contact = db.contacts.find(id); if (contact == null) { homecoming httpnotfound(); } homecoming view(contact); } // post: /ct/edit/5 // protect overposting attacks, please enable specific properties want bind to, // more details see http://go.microsoft.com/fwlink/?linkid=317598. [httppost] [validateantiforgerytoken] public actionresult edit([bind(include="contactid,dateintake,namefirst")] contact contact) { if (modelstate.isvalid) { db.entry(contact).state = entitystate.modified; db.savechanges(); homecoming redirecttoaction("index"); } homecoming view(contact); } // get: /ct/delete/5 public actionresult delete(int? id) { if (id == null) { homecoming new httpstatuscoderesult(httpstatuscode.badrequest); } contact contact = db.contacts.find(id); if (contact == null) { homecoming httpnotfound(); } homecoming view(contact); } // post: /ct/delete/5 [httppost, actionname("delete")] [validateantiforgerytoken] public actionresult deleteconfirmed(int id) { contact contact = db.contacts.find(id); db.contacts.remove(contact); db.savechanges(); homecoming redirecttoaction("index"); } protected override void dispose(bool disposing) { if (disposing) { db.dispose(); } base.dispose(disposing); } } } here's contacts index page:
@model ienumerable<basiccontacts.models.contact> @{ viewbag.title = "index"; } <h2>index</h2> <p> @html.actionlink("create new", "create") </p> <table class="table"> <tr> <th> @html.displaynamefor(model => model.dateintake) </th> <th> @html.displaynamefor(model => model.namefirst) </th> <th></th> </tr> @foreach (var item in model) { <tr> <td> @html.displayfor(modelitem => item.dateintake) </td> <td> @html.displayfor(modelitem => item.namefirst) </td> <td> @html.actionlink("edit", "edit", new { id=item.contactid }) | @html.actionlink("details", "details", new { id=item.contactid }) | @html.actionlink("delete", "delete", new { id=item.contactid }) </td> </tr> } </table> obviously there shared layout page.
so how create "stats" model , controller in visual studio , should in able display sql query counts records in "contacts?" , bonus if utilize mvc html helpers seems consistent current architecture.
personally, don't calculations in view (personal preference).
i counts/aggregations/sums/percentages in controller action stats , create model contains properties each statistic want display.
i create stats view html helpers display these model properties within view.
let me know if doesn't create sense. hth
c# sql asp.net-mvc visual-studio
Comments
Post a Comment