Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Passing model as list in MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 357
    Comment on it

    In MVC while making projects you need to pass the model of attributes as a list.

     

    For this you need to pass or return it in form of list and then bind it with the action which will invoke that function and at last associate it with the view.

     

     public EmployeeModel EditDetails(EmployeeModel EmpMod)
            {
                try
                {
                    if (EmpMod.EmpId > 0)
                    {
                        SqlDataReader reader = null;
                        using (SqlConnection sqlConnection = new SqlConnection(strConnString))
                        {
                            List<SqlParameter> parameterList = new List<SqlParameter>();
                            parameterList.Add(new SqlParameter("@empId", EmpMod.EmpId));
                            if (sqlConnection.State == ConnectionState.Closed)
                            {
                                sqlConnection.Open();
                            }
                            reader = SqlHelper.ExecuteReader(sqlConnection, CommandType.StoredProcedure, "uspEditUser", parameterList.ToArray());
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    EmpMod.FirstName = reader["EmpFirstName"] != null ? Convert.ToString(reader["EmpFirstName"]) : String.Empty;
                                    EmpMod.MiddleName = reader["EmpMiddleName"] != null ? Convert.ToString(reader["EmpMiddleName"]) : String.Empty;
                                    EmpMod.LastName = reader["EmpLastName"] != null ? Convert.ToString(reader["EmpLastName"]) : String.Empty;
                                    EmpMod.PhoneNo = reader["EmpPhoneNo"] != null ? Convert.ToString(reader["EmpPhoneNo"]) : String.Empty;
                                    EmpMod.Address = reader["EmpAddress"] != null ? Convert.ToString(reader["EmpAddress"]) : String.Empty;
                                    EmpMod.Status = getstatus.success;
                                    break;
                                }
                            }
                            else
                            {
                                EmpMod.Status = getstatus.failure;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    EmpMod.msg = ex.Message;
                }
                return EmpMod;
            }
    
    
    

     

    In this we passed the Employee Model as a list and receiving the model as list so this will be binded in our action and then in our view.

     

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

     

 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: