• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 49
  • Last Modified:

MVC Display all records

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
RadhaKrishnaKiJaya
Asked:
RadhaKrishnaKiJaya
  • 4
  • 3
1 Solution
 
Fernando SotoCommented:
Show your View code for the Details action please.
0
 
RadhaKrishnaKiJayaAuthor Commented:
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
 
RadhaKrishnaKiJayaAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Fernando SotoCommented:
In the Student class please post the Namespace in that file.
0
 
Fernando SotoCommented:
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
 
RadhaKrishnaKiJayaAuthor Commented:
Thank you very much.
0
 
Fernando SotoCommented:
Not a problem RadhaKrishnaKiJaya, glad I was able to help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now