简体   繁体   中英

How do I check how many records of a database meets the right criteria in ASP.NET MVC?

When the admin presses the 'conlude' button in the concluded view, I want the program to go through every record in the database, and check if exactly 2 records, no more no less, has candidate set to true. (rest will be false). I thought I could do this through a count loop, which goes through every record and checks whether candidate is set to true or false, but how do I implement this in ASP.NET MVC??

Thank you for your time

stored procedure (dbo.conclude)

ALTER PROCEDURE [dbo].[conclude]
AS
BEGIN
    SET NOCOUNT ON;

    delete from dbo.Applications
    where Candidate=0
END

action/method - edit: new code

 public ActionResult Conclude(Concluded concluded, Application application)
        {
            var count = 0;
            if (count != 2)
            {
                foreach (NEA.Models.Application app in db.Applications.ToList())
                {
                    if (app.Candidate == true)
                    {
                        count++;
                    }
                }
            }
            if (count == 2)
            {
                using (var context = new ApplicationDbContext())
                {
                    var none = context.Applications.SqlQuery("dbo.conclude");

                    return View(none);
                }
            }
            return View("NotSaved");
        }

error message i get: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbSqlQuery`1[NEA.Models.Application]', but this dictionary requires a model item of type 'NEA.Models.Application'.

EDIT 2 I figured put the problem. In my view, I simply put @model NEA.Models.Application when I had to put @model IEnumerable<NEA.Models.Application> Thanks for the comments and help:)

When the admin presses the 'conlude' button in the concluded view, I want the program to go through every record in the database, and check if exactly 2 records, no more no less, has candidate set to true. (rest will be false). I thought I could do this through a count loop, which goes through every record and checks whether candidate is set to true or false, but how do I implement this in ASP.NET MVC??

Thank you for your time

stored procedure (dbo.conclude)

ALTER PROCEDURE [dbo].[conclude]
AS
BEGIN
    SET NOCOUNT ON;

    delete from dbo.Applications
    where Candidate=0
END

action/method - edit: new code

 public ActionResult Conclude(Concluded concluded, Application application)
        {
            var count = 0;
            if (count != 2)
            {
                foreach (NEA.Models.Application app in db.Applications.ToList())
                {
                    if (app.Candidate == true)
                    {
                        count++;
                    }
                }
            }
            if (count == 2)
            {
                using (var context = new ApplicationDbContext())
                {
                    var none = context.Applications.SqlQuery("dbo.conclude");

                    return View(none);
                }
            }
            return View("NotSaved");
        }

error message i get: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbSqlQuery`1[NEA.Models.Application]', but this dictionary requires a model item of type 'NEA.Models.Application'.

EDIT 2 I figured put the problem. In my view, I simply put @model NEA.Models.Application when I had to put @model IEnumerable<NEA.Models.Application> Thanks for the comments and help:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM