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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 32
    Comment on it

    While working into the MVC project you always return ActionResult from the controller action.

     

     

    But there are scenarios when we want to return other things like list and in that case we need to make changes in our view to do that.

     

     

    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; }
        }

     

     

     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;
            }
    
        }
    
    
    

     

    This is the view we have made for binding it with the database.

     

     

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

     

     

    We can see that we are passing data here of list type so the receiving end of our view should be changed to accept this kind of data.

     

    @model IEnumerable<Modal_PopUp_Basic.Models.EmployeeModel>
    
    @{
        ViewBag.Title = "Index";
    }
    
    

     

    .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: