Using SQL Query with Asp MVC5

Asked

Viewed 443 times

0

how do I execute an SQL query with ASP MVC5 + Entity?

Example, I need to use "Count" in a table called "templates" am dev php and am learning now Asp.net framework mvc 5 and have no idea how to do this...

To create the MVC I used the option of Entity itself (I created the model, context) ai already generated the controller and the views

Follows code from 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;

namespace TeZ.Models
{
    public class MembersController : Controller
    {
        private MembersContext db = new MembersContext();

        // GET: Members
        public ActionResult Index()
        {
            return View(db.members.ToList());
        }

        // GET: Members/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Member member = db.members.Find(id);
            if (member == null)
            {
                return HttpNotFound();
            }
            return View(member);
        }

        // GET: Members/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Members/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "mbMatricula,mbName,mbFunc,mbMail,mbPic,mbPass,mbActive")] Member member)
        {
            if (ModelState.IsValid)
            {
                db.members.Add(member);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(member);
        }

        // GET: Members/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Member member = db.members.Find(id);
            if (member == null)
            {
                return HttpNotFound();
            }
            return View(member);
        }

        // POST: Members/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "mbMatricula,mbName,mbFunc,mbMail,mbPic,mbPass,mbActive")] Member member)
        {
            if (ModelState.IsValid)
            {
                db.Entry(member).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(member);
        }

        // GET: Members/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Member member = db.members.Find(id);
            if (member == null)
            {
                return HttpNotFound();
            }
            return View(member);
        }

        // POST: Members/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Member member = db.members.Find(id);
            db.members.Remove(member);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
  • if you have the code of any Controller?

  • I have yes I will be editing and putting

  • private MembersContext db = new MembersContext(); this is your correct ORM class so it would be db.modelos.count() ....

  • However much the Enumerable.Count() resolve, you can make raw SQL query using the .Database.SqlQuery("SELECT * ...") if the example was hypothetical and you want to do another query.

  • But then, I put this data where? In the controller before returning to the view? And what do I call the data in the view? I’m pretty dumb about Asp and Entity use Laravel hehe a lot

  • Thanks a lot for the help, I already got =) only use @Model.Count() inside the Tks view

Show 1 more comment

1 answer

2

I will give an answer based on what you said in the comments.

First, to run a Count in a database table and return its value, you can use the method Count in the Dbset that represents the table (members is the Dbset in this case).

For this value to be "sent" to a view, you need to specify this in the method call View().

public class MembersController : Controller
{
    private MembersContext db = new MembersContext();

    public ActionResult Contador()
    {
        var count = db.members.Count();
        return View(count);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.