Solved

MVC Display all records

Posted on 2016-07-14
7
42 Views
Last Modified: 2016-07-14
Hi Experts,

I am a real beginner in MVC. I am getting all the records from the DB. How can I display it in a view. In the code I am sending it to the view. But how can I display all the records.

public ActionResult Details()
        {
            StudentContext studentcontext = new StudentContext() ;
            return View(studentcontext.Students);
        }

Thanks in advance.
0
Comment
Question by:RadhaKrishnaKiJaya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41710817
Show your View code for the Details action please.
0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 41710821
Thanks for your reply. Actually I don't know what to do.

Thanks.

@model  DatabaseConnection.Models.Student  
@{
    ViewBag.Title = "Details";
}

<h2>View Students</h2>

<table>
    <tr>
        <td> @Model.UserID</td>        
    </tr>

    <tr>
        <td> @Model.UserFirstName </td>
    </tr>

    <tr>
        <td> @Model.UserLastName </td>
    </tr>
</table>
0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 41710822
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace DatabaseConnection.Models
{
    public class StudentContext : DbContext
    {
        public DbSet<Student> Students { get; set; }
    }
}
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41710847
In the Student class please post the Namespace in that file.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41710882
Hi RadhaKrishnaKiJaya;

Try your View as follows. In the @model statement below replace "DatabaseConnection.Models.Student" with the namespace in the Student class if this is not correct.
@model  IEnumerable<DatabaseConnection.Models.Student>  

@{
    ViewBag.Title = "Details";
}

<h2>View Students</h2>

<table>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.UserID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.UserFirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.UserLastName)
        </td>
    </tr>
</table>

Open in new window

0
 

Author Closing Comment

by:RadhaKrishnaKiJaya
ID: 41710976
Thank you very much.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41710983
Not a problem RadhaKrishnaKiJaya, glad I was able to help.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Get Current Exchange Rate from XE 9 39
C# DateTime Help 6 44
Read top line from CSV file 14 55
How does GeoJson relates to Leaflet and Mapbox? 4 43
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question