Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
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
Comments
Jameel VM 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 VM 10-May-13 5:24am    
i have updated my answer.Please look at this
Jameel VM 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<yournamespace.models.employee>@{} and iterate like @foreach (var item in Model)

1 solution

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.
C#
@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

C#
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
 
Share this answer
 
v2

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