Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to much data in my table which I bind in my dropdown, but now I want just those data which i want. Can i implement select query here?
I want some of my data according to my select query, like i want "acc_no" where acc_des="XYZ".

What I have tried:

This is my controller and i bind table like this...
C#
var accounts = db.gl_m_acc.ToList();
            foreach (var item in accounts)
            {
                GetAccountsCH ac = new GetAccountsCH();
                ac.acc_no = item.acc_no;
                ac.acc_des = item.acc_des;
                Gl_t_Vcr_model.acc.Add(ac);
            }


This is my View:

HTML
@Html.DropDownListFor(model => model.acc_no, new SelectList(Model.acc, "acc_no","acc_des"), "<---Select--->", new { @class = "form-control", style = "width: 100%;" })


This is my Model:
C#
public class GetAccountsCH
    {
        public int acc_no { get; set; }

        public string acc_des { get; set; }
    }
 public class Gl_t_vcr
    {
  public List<getaccountsch> acc = new List<getaccountsch>();
      }
Posted
Updated 3-Apr-19 5:49am
v3
Comments
#realJSOP 3-Apr-19 7:18am    
Your variable and method names absolutely suck.

Just sayin...

1 solution

Your code is too hard to follow due to abbreviations used as variable names. I can't easily tell whats what.

So here is how you can filter data in the controller for use in your view. You'll need to make one or two modifications to your model.

Controller
C#
public ActionResult Index ()
{
    var model = new MyModel();
    var getData = database.MyTable.Where(m=>m.AccountDescription = "Savings");

    foreach(var item in getData)
    {
       //I did not put this code into a compiler, i may have the SelectListItem property names incorrect
       var selectItem = new SelectListItem();
       //Sets what is displayed in the drop down
       selectItem.Text = item.AccountNumber;
       //Sets the value to capture
       selectItem.Value = item.AccountNumber;
       model.DropDown.Add(selectItem);
    }
    return View(model);
}


Model

C#
public class MyModel
{
    public MyModel()
    {
        this.DropDown = new List<SelectListItem>();
    }

    public string AccountNumber {get;set;}
    public List<SelectListItem> DropDown {get;set;}
}


View

HTML
@Html.DropDownListFor(model => model.AccountNumber , model.DropDown, "<---Select--->", new { @class = "form-control", style = "width: 100%;" })


This would be how you can send data from your data store to the view as a drop down. And specifically, if i understand your question, the .Where( clause in the controller is how you filter the data to fetch just the info you want.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900