Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Framework entity MVC3
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 21:36pm
Comments
Jameel Moideen at 10-May-13 5:10am
   
Please post your controller code..
Ram7 from Hyderabad at 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 at 10-May-13 5:24am
   
i have updated my answer.Please look at this
Jameel Moideen at 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>
 <th>
            @Html.DisplayNameFor(model => model.EmpName)
        </th>
 <th>
            @Html.DisplayNameFor(model => model.Designation)
        </th>
 <th>
            @Html.DisplayNameFor(model => model.Salary)
        </th>
 <th>
            @Html.DisplayNameFor(model => model.DeptNo)
        </th>
        <th></th>
    </tr>
 
@foreach (var item in Model.getObj) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.EmpID)
        </td>
<td>
            @Html.DisplayFor(modelItem => item.EmpName)
        </td>
<td>
            @Html.DisplayFor(modelItem => item.Designation)
        </td>
<td>
            @Html.DisplayFor(modelItem => item.Salary)
        </td>
<td>
            @Html.DisplayFor(modelItem => item.DeptNo)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.EmpID }) |
            @Html.ActionLink("Details", "Details", new { id=item.EmpID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.EmpID })
        </td>
    </tr>
}
 
</table>
 
//for Editing your actionResult like below
 
[HttpGet]
public ActionResult Edit(int EmpID )
{
}
 

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
0 OriginalGriff 319
1 Kornfeld Eliyahu Peter 265
2 Maciej Los 249
3 BillWoodruff 205
4 Sergey Alexandrovich Kryukov 175
0 OriginalGriff 5,107
1 DamithSL 4,157
2 Maciej Los 3,595
3 Kornfeld Eliyahu Peter 3,450
4 Sergey Alexandrovich Kryukov 2,741


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 10 May 2013
Copyright © CodeProject, 1999-2014
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