Friday, 17 April 2015

ASP.NET + MVC

IMPLEMENT CHECKBOOK DYNAMICALLY FROM DATABASE MVC


FIRST CREATE CLASS 
eg-Faculty

1.then declare another class for checkbox in that only eg -department

demo-

 public class department
    {
        public string deptid { get; set; }
        public string deptname { get; set; }
        public bool checkedstatus { get; set; }
    }

 public class Faculty:department
    {
      public List<department> departmentlist { get; set; }
    }

2.then create a controller Faculty
then write the following list method



  public List<department> Binddepartment()
        {

            List<department> objdpartment = new List<department>();
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
            SqlCommand cmd = new SqlCommand("select * from tblDEPARTMENT where DP_STATUS='1' OR DP_STATUS='2'", con);
            con.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                department DEPT = new department();
                DEPT.deptname = sdr["DP_NAME"].ToString();
                objdpartment.Add(DEPT);

            }
            return (objdpartment);

        }

3.then paste these code in your get property

  public ActionResult check()
        {
            Facultyall fa = new Facultyall();
            fa.departmentlist = new List<department>();
            fa.departmentlist = Binddepartment();
            return View(fa);
        }

4. then paste this in your post properties

  StringBuilder sb = new StringBuilder();
            foreach (var item in fa.departmentlist)
            {
                if (item.checkedstatus == true)
                {
                    sb.Append(item.deptname + ",").AppendLine();
                }
            }
            sb.Remove(sb.ToString().LastIndexOf(","), 1);

Note: here sb.tostring will contain your selected checkboxes

then in your view ,write the following code
 <div class="editor-field">
                       @for (int i = 0; i < Model.departmentlist.Count; i++)
                      {
                         
                    @Html.CheckBoxFor(m => Model.departmentlist[i].checkedstatus)
                            @Model.departmentlist[i].deptname
                
                    @Html.HiddenFor(m => Model.departmentlist[i].deptid)
                    @Html.HiddenFor(m => Model.departmentlist[i].deptname)
                        
                      }
                     
                    </div>

Note: at last again return the check method in post properties
just copy and paste the check method before return view()


thats all..thanks for reading...

No comments:

Post a Comment