Solved

MVC Display all records

Posted on 2016-07-14
7
30 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
Comment Utility
Show your View code for the Details action please.
0
 

Author Comment

by:RadhaKrishnaKiJaya
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 62

Expert Comment

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

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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
Comment Utility
Thank you very much.
0
 
LVL 62

Expert Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

17 Experts available now in Live!

Get 1:1 Help Now