如何使用EntityFramework

如何使用EntityFramework

本文介绍了如何使用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

推荐答案

@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在视图中检索和显示完整的表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 11:12