Saturday, 18 April 2015

IMPLEMENT SEARCH USING 3 DROPDOWNLIST IN ASP.NET MVC

eg-searching of student of basis of year,section,class


1.create a CLASS ModalBussinessDataLayersave

DECLARE FETCHING METHOD FOR SECTION AND CLASS

table:-tbl_SECTION
SC_ID    nvarchar(50)    Unchecked
SC_NAME    nvarchar(50)    Checked

  public DataSet BindSection()
        {

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
            SqlCommand cmd = new SqlCommand("select * from tblSECTION", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }


TABLE-tblCLASS
CL_ID    nvarchar(20)    Unchecked
CL_NAME    nvarchar(100)    Checked

        public DataSet BindCLASS()
        {

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
            SqlCommand cmd = new SqlCommand("select * from tblCLASSNAME", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }

THEN CREATE THE all DROPDOWNLIST METHOD
YEAR:-
  public ActionResult Bindyears()
        {
            List<SelectListItem> items = new List<SelectListItem>();
            for (int i = 1980; i <= 2030; i++)
            {
                items.Add(new SelectListItem { Text = i.ToString(), Value = i.ToString() });
            }
            ViewBag.years = items;
            return View();
        }
 
 FOR section:

method-
 public ActionResult BindSections()
        {


            ModalBussinessDataLayersave stc = new ModalBussinessDataLayersave();
            DataSet ds = stc.BindSection();
            ViewBag.fname = ds.Tables[0];
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (DataRow dr in ViewBag.fname.Rows)
            {
                items.Add(new SelectListItem { Text = @dr["SC_NAME"].ToString(), Value = @dr["SC_NAME"].ToString() });
            }
            ViewBag.SC_NAME = items;
            return View();
        }
FOR CLASS:
method:
 public ActionResult Bindclass()
        {

            ModalBussinessDataLayersave stc = new ModalBussinessDataLayersave();
            DataSet ds = stc.BindCLASS();
            ViewBag.fname = ds.Tables[0];
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (DataRow dr in ViewBag.fname.Rows)
            {
                items.Add(new SelectListItem { Text = @dr["C_NAME"].ToString(), Value = @dr["C_NAME"].ToString() });
            }
            ViewBag.C_NAME = items;
            return View();
        }

then create method for search eg-
 [HttpGet]
        public ActionResult studentattendanceview(int? page, string years, string C_NAME, string SC_NAME, string search = "a")
        {
           
            BindSections();
            Bindclass();
            Bindyears();
            ModalBussinessDataLayersave bdl = new ModalBussinessDataLayersave();
         
            return View(bdl.scd.Where(x =>x.CD_SYEAR == years && x.CD_SCLASS == C_NAME && x.CD_SSECTION == SC_NAME ).ToList();
        
        }
Then finally in view
paste the following

@using(Html.BeginForm("studentattendanceview", "StudentClassDetail",FormMethod.Get))
{
<b>search by</b>  @Html.DropDownList("years", "Select")<text>years</text>   @Html.DropDownList("C_NAME", "Select") <text>class</text>      @Html.DropDownList("SC_NAME", "Select") <text>section</text>
   <input type="submit" value="Search" />
}

THATS ALL
thanks for reading


No comments:

Post a Comment