Solved

MVC Display all records

Posted on 2016-07-14
7
36 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

815 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

8 Experts available now in Live!

Get 1:1 Help Now