Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Binding data in the form of List in MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 33
    Comment on it

    For binding data we can have data that contains single records while fetching with the data source or multiple rows.

     

    If we have single row the fetching and display is very simple , but if the number of records are multiple then we have to pass it in the form of the list.

     

      public enum getstatus
        {
            success, failure
        }
        public class EmployeeModel
        {
    
            public int EmpId { get; set; }
            [Required(ErrorMessage = "First Name is required")]
            public string FirstName { get; set; }
            public string MiddleName { get; set; }
            [Required(ErrorMessage = "Last Name is required")]
            public string LastName { get; set; }
            [Required(ErrorMessage = "PhoneNo is required")]
            public string PhoneNo { get; set; }
            public string Address { get; set; }
            public getstatus Status { get; set; }
            public string msg { get; set; }
        }

     

    This is my model for the table which i want to fetch data.

     

      public List<EmployeeModel> GetDetails()
            {
                List<EmployeeModel> EmployeeLst = new List<EmployeeModel>();
                try
                {
                    SqlDataReader reader = null;
                    using (SqlConnection sqlConnection = new SqlConnection(strConnString))
                    {
                        if (sqlConnection.State == ConnectionState.Closed)
                        {
                            sqlConnection.Open();
                        }
                        reader = SqlHelper.ExecuteReader(sqlConnection, CommandType.StoredProcedure, "uspGetUsers");
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                EmployeeLst.Add(new EmployeeModel
                                {
                                    EmpId = reader["EmpId"] != null ? !string.IsNullOrEmpty(Convert.ToString(reader["EmpId"])) ? Convert.ToInt32(Convert.ToString(reader["EmpId"]).Trim()) : 0 : 0,
                                    FirstName = reader["EmpFirstName"] != null ? Convert.ToString(reader["EmpFirstName"]) : String.Empty,
                                    MiddleName = reader["EmpMiddleName"] != null ? Convert.ToString(reader["EmpMiddleName"]) : String.Empty,
                                    LastName = reader["EmpLastName"] != null ? Convert.ToString(reader["EmpLastName"]) : String.Empty,
                                    PhoneNo = reader["EmpPhoneNo"] != null ? Convert.ToString(reader["EmpPhoneNo"]) : String.Empty,
                                    Address = reader["EmpAddress"] != null ? Convert.ToString(reader["EmpAddress"]) : String.Empty,
                                    Status = getstatus.success
                                });
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    EmployeeLst.Add(new EmployeeModel
                    {
                        msg = ex.Message,
                        Status = getstatus.failure
                    });
                }
                return EmployeeLst;
            }
    
        }

     

    In this , i have made the list that will return the model class records that i have created into the action where this method will be called.

     

     EmployeeRepository EmpRepObj = null;
    
            public ActionResult Index()
            {
                EmpRepObj = new EmployeeRepository();
                List<EmployeeModel> EmpMod = new List<EmployeeModel>();
                EmpMod = EmpRepObj.GetDetails().ToList();
                return View(EmpMod);
            }

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: