Click here to Skip to main content
12,405,543 members (64,094 online)
Rate this:
 
Please Sign up or sign in to vote.
Hii to all,
 i have a requirement that i need to retrive& display a complete table data present in a sqlserver database in a view using Entity Framework & MVC3.Is there any controle like Gridview in MVC3,for displaying a table data.Please guide me how can i show a complete data in view.
This is My Model Employee:-
public class Employee
    {
        public int EmpID { get; set; }
        public String EmpName { get; set; }
        public String Designation { get; set; }
        public int Salary { get; set; }
        public int DeptNo { get; set; }
    }
------------------------------------------------------------
This is My Model Get Data:-
public class TableData
    {
        public List<Employee> getobj { get; set; }
    }
----------------------------------------------------------------
yet i need to write my controller code,
Please guide me how to write my View code,just i tried by writing Foreach Loop,but didn't reached my requirement
This is My View:-
 @using (Html.BeginForm(FormMethod.Post))
{
 
    <fieldset>
        <legend>EmployeeInformation</legend>
        <table>
        <tr>
   @foreach(var item in Model.getobj)
   {
 
     <td>item.EmpID;</td>
   }
   </tr>
   </table>
 </fieldset>
}
 Iam trying to add each item to the TD but iam unable to access item value  with in the TD,please correct my View..Thanks in advance
 
Thanks
Ramu
Posted 9-May-13 20:36pm
Comments
Jameel Moideen 10-May-13 5:10am
   
Please post your controller code..
Ram7 from Hyderabad 10-May-13 5:17am
   
Hi,Following is my Controller Code
public ActionResult GetData(TableData model)
{
using (personalEntities db = new personalEntities())
{
var emply = from p in db.empdetails
select new
{
EmpID = p.empid,
EmpName = p.ename,
Designation = p.designation,
Salary = p.salary,
DeptNo = p.deptno,
};
}
return View(emply.ToList());
}
In the Last line of my Controller Code i.e return View(emply.ToList()),please observer this line wheather iam doing write or not..
Thanks
Ramu
Jameel Moideen 10-May-13 5:24am
   
i have updated my answer.Please look at this
Jameel Moideen 10-May-13 5:29am
   
if you want to return emply.ToList() to a view you must specify view return model as IEnumerable type like this @model IEnumerable@{} and iterate like @foreach (var item in Model)

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Please Replace this mark up and try. In MVC there is not default gridview. We need to customize our own format else use some third
party plugins like jQgrid.
@model YourNameSpace.Models.TableData
 
@{
    ViewBag.Title = "_TableData";
}
 
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.EmpID)
        </th>


Replace your Controller like below

public ActionResult GetData(TableData model)
        {
            using (personalEntities db = new personalEntities())
 {
                var emply = from p in db.empdetails
                            select new
                            {
                                EmpID = p.empid,
                                EmpName = p.ename,
                                Designation = p.designation,
                                Salary = p.salary,
                                DeptNo = p.deptno,
                            };
            }
 
model=new TableData
{
getObj=emply.ToList()
};
            return View(model);
        }

Hope this helps
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 10 May 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100