Solved

MVC Display all records

Posted on 2016-07-14
7
34 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
  • 4
  • 3
7 Comments
 
LVL 62

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 62

Expert Comment

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

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 62

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now