How to retrive&dispaly Complete Table data in a view using EntityFramework MVC3 [Resolved]

Posted by ramuvalmiki07-22001 under ASP.NET on 5/10/2013 | Points: 10 | Views : 10362 | Status : [Member] | Replies : 5
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




Responses

Posted by: Satyapriyanayak on: 5/10/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
http://www.c-sharpcorner.com/UploadFile/cd3310/createeditdelete-operation-on-a-table-in-Asp-Net-mvc-appli/
http://weblogs.asp.net/dotnetstories/archive/2011/08/07/retrieving-data-from-an-sql-server-database-in-an-mvc-3-application.aspx
http://rohangayakwad90.wordpress.com/2013/03/02/simple-insertupdatedeletedisplay-in-mvc/

If this post helps you mark it as answer
Thanks

ramuvalmiki07-22001, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 5/10/2013 [Member] Silver | Points: 50

Up
0
Down

Resolved
Hi,

In MVC 3, you have a WebGrid control where you can display the data.
Here is the code...

Write the following code in your ViewPage....

@{
ViewBag.Title = "Purchased";
}
<h2>
Purchased</h2>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>LIST OF PURCHASED</title>
<style type="text/css">
.gridTable
{
margin: 5px;
padding: 10px;
border: 1px #c8c8c8 solid;
border-collapse: collapse;
min-width: 550px;
background-color: #fff;
color: #fff;
}
.gridHead th
{
font-weight: bold;
background-color: #000000;
color: #fff;
padding: 10px;
}
.gridHead a:link, .gridHead a:visited, .gridHead a:active, .gridHead a:hover
{
color: #FFBBFF;
}
.gridHead a:hover
{
text-decoration: underline;
}
.gridTable tr.gridAltRow
{
background-color: #efeeef;
}
.gridTable tr:hover
{
background-color: #FFCCCC;
}
.gridAltRow td
{
padding: 10px;
margin: 5px;
color: #333;
}
.gridRow td
{
padding: 10px;
color: #333;
}
.gridFooter td
{
padding: 10px;
background-color: #c7d1d6;
color: #999;
font-size: 12pt;
text-align: center;
}
.gridFooter a
{
font-weight: bold;
color: #333;
border: 1px #333 solid;
}
.</style>
</head>
<body>
@*<script src="../../Scripts/jquery-1.3.2-vsdoc2.js" type="text/javascript"></script>*@
<div id="divmsg" style="color: Green; font-weight: bold">
</div>
<br />
@{
var grid = new WebGrid(Model, canPage: true, rowsPerPage: 10);

grid.Pager(WebGridPagerModes.NextPrevious);

@grid.GetHtml(tableStyle: "gridTable",
htmlAttributes: new { id = "DataTable" },
headerStyle: "gridHead",
footerStyle: "gridFooter",
rowStyle: "gridRow",
alternatingRowStyle: "gridAltRow",
columns: grid.Columns(


grid.Column("EmpID"),

grid.Column("EmpName"),

grid.Column("Designstion"),

grid.Column("Salary"),

grid.Column("DeptNo")
));
}
</body>
</html>

Then, in the controller write the following code..............

public ActionResult Purchased()
{
var emply= from p in db.TableName
select new
{
EmpID= p.EmpID,
EmpName= p.EmpName,
Designation= p.Designation,
Salary= p.Salary,
DeptNo= p.DeptNo,
};
return View(emply.ToList());
}

That's it........

Mark as answer if satisfied............

Regards,
Shree M.


Kavya Shree Mandapalli

ramuvalmiki07-22001, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 5/10/2013 [Member] Silver | Points: 50

Up
0
Down

Resolved
Hi,

When you create your MVC application, select empty application and others like (basic,intranet,internet......).
Uncheck the create strongly typed check box..
Now run the application and see........

Mark as Answer if satisfied.............

Regards,
Shree M.

Kavya Shree Mandapalli

ramuvalmiki07-22001, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: ramuvalmiki07-22001 on: 5/10/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Shree,
Thanks for Quick Responce, i have tried with ur code,but iam finding the below error at the following line in view code
Error Line in VIew:- grid.Pager(WebGridPagerModes.NextPrevious);
saying that "A data source must be bound before this operation can be performed",
Please tell me how to overcome this error
Thanks
Ramu

ramuvalmiki07-22001, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: ramuvalmiki07-22001 on: 5/10/2013 [Member] Starter | Points: 25

Up
0
Down
Hii Shree,
Now its working fine,thanks for all...............
Can you provide Any Links for,how to work or how to add jQgrid in our applications,i want to perform the above same with jQgrid....

Thanks
Ramu

ramuvalmiki07-22001, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response