AMIT KUMAR
AMIT KUMAR

Reputation: 1

How can i select specific records from database in asp MVC?

In the following code, I want to retrieve all those records whose Q_ID equals to the ID. Currently, I Select whole records instead of few. Please help me to solve this problem

    public ActionResult Solution(int ID)
    {
        Answers ans_obj = new Answers();

        List<Ans_Table> dbobj= db.Ans_Table.ToList();

        List<Answers> ansobj = dbobj.Select(x => new Answers
        {
            Answer = x.Answer,
            Q_ID=x.Q_ID,
            U_ID=x.U_ID

        }).ToList();

        return View();
    }

Upvotes: 0

Views: 39

Answers (1)

Marco
Marco

Reputation: 23937

Have you tried a simple Where()?

public ActionResult Solution(int ID)
{
    List<Ans_Table> dbobj = db.Ans_Table.Where(x => x.Q_ID == ID).ToList();

    List<Answers> ansobj = dbobj.Select(x => new Answers
    {
        Answer = x.Answer,
        Q_ID=x.Q_ID,
        U_ID=x.U_ID
    }).ToList();

    return View(ansobj);
}

Side Note: You need to pass something into your View, or it will not display any data. You should read the basics of Asp.Net MVC

Upvotes: 1

Related Questions