如何使用EntityFramework MVC3在视图中检索和显示完整的表数据 [英] How to retrive&dispaly Complete Table data in a view using EntityFramework MVC3

查看:90
本文介绍了如何使用EntityFramework 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

推荐答案

请替换此标记并尝试。在MVC中没有默认的gridview。我们需要自定义我们自己的格式,否则使用第三个

派对插件,比如jQgrid。

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


这篇关于如何使用EntityFramework MVC3在视图中检索和显示完整的表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆