Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In Controller:

C#
public ActionResult Employee()
       {
           //List<CustomerInfo> oCustomerInfos = DataLayer.GetALLCustomers();
           List<Employee> oEmpInfos = DB.GetALLEmpDetails();

           var myResult = new
           {
               //CustomerList = oCustomerInfos,
               ProductList = oEmpInfos

           };
           return View(oEmpInfos); // return list of employee


       }


in Data Layer:

C#
public static List<employee> GetALLEmpDetails()
       {
           try
           {
               string connections = System.Configuration.ConfigurationManager.ConnectionStrings["EmployeeContext"].ToString();
               SqlConnection conn = new SqlConnection(connections);
               string query = "select * from Employee";

               SqlCommand cmd = new SqlCommand(query, conn);
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               DataTable dtEmployees = new DataTable();
               da.Fill(dtEmployees);

               List<employee> oEmpInfos = new List<employee>();
               if (dtEmployees != null && dtEmployees.Rows.Count > 0)
               {
                   foreach (DataRow dr in dtEmployees.Rows)
                   {
                       Employee oEmpInfo = new Employee();
                       oEmpInfo.EmpId = Convert.ToInt32(dr["EmpId"]);
                       oEmpInfo.EmpName = dr["EmpName"].ToString();
                       oEmpInfo.Designation = dr["Designation"].ToString();
                       oEmpInfo.Percentage1 = Convert.ToInt32(dr["Percentage1"]);
                       oEmpInfo.Percentage2 = Convert.ToInt32(dr["Percentage2"]);
                       oEmpInfo.Location = dr["Location"].ToString();
                       oEmpInfos.Add(oEmpInfo);
                   }
               }
               return oEmpInfos;
           }
Posted
Updated 12-Nov-15 3:26am
v2

Good, you have data in the action method. Now bind that data to its view.

ASP
@model IEnumerable<Employee>

<ul>
@foreach(var item in Model)
{
    <h3>@item.EmpName</h3>
    <p>Designation: @item.Designation
    <br />Location: @item.Location
    </p>
    
    <hr />
}
</ul>

This will list out your all the employee details. :)

Refer this-
Passing Data in an ASP.NET MVC[^]
Passing data from Controller to View in ASP.NET MVC[^]


-KR
 
Share this answer
 
v2
Comments
Member 10403555 12-Nov-15 9:56am    
It giving error like this: Only one 'model' statement is allowed in a file.

i'm writing post method also in my html
Member 10403555 12-Nov-15 10:01am    
the code you given is worked when i removes first @model which is given for posting data refrence

Is there any problem posting and getting data in the same HTML
Krunal Rohit 12-Nov-15 10:38am    
Okay.
No, there's nothing wrong.

-KR

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900