Categories
Asp, Asp.net

MVC Tutorial Display Student Information

MVC Tutorial Display Student Information

In this tutorial we will learn MVC Tutorial Display Student Information, here we will display student information from models. Earlier we have learnt How To Create New Asp.Net MVC 5 Site From Empty Template

1. Create a new project with asp.net mvc from empty template.

2. Right click on Models folder > Add > Class, Name the class as Student.cs, Click Add.

public class Student
{
    public int StudentId { get; set; }
    public int Studentrollno { get; set; }
    public string Name { get; set; }
    public string Gender { get; set; }
    public string City { get; set; }
}

3. Now let’s Add StudentController class to Controllers folder. To do this Right click on Controllers folder > Add > Controller. Use StudentController as the name Click Add.

4. Build your project and then Right click on Index() action method and select Add View from the context menu. Choose Student from the model class dropdownlist.

5. Now add following code to your view.

<table style="font-family:Arial">

<tr>
<td>Student ID:</td><td>@Model.StudentId</td>
</tr>

<tr>
<td>Student Roll no.:</td><td>@Model.Studentrollno</td>
</tr>

<tr>
<td>Name:</td><td>@Model.Name</td>
</tr>

<tr>
<td>Gender:</td><td>@Model.Gender</td>
</tr>

<tr>
<td>City:</td><td>@Model.City</td>
</tr>

</table>

6. Now run your project, if you got following error: Object reference not set to an instance of an object. Then pass student object to the view.

Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVC_Tutorial_Display_Student_Information.Models
{
    public class Student
    {
        public int StudentId { get; set; }
        public int Studentrollno { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string City { get; set; }
    }
}

Controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC_Tutorial_Display_Student_Information.Models;

namespace MVC_Tutorial_Display_Student_Information.Controllers
{
    public class StudentController : Controller
    {
        //
        // GET: /Student/
        public ActionResult Index()
        {
            Student student = new Student()
            {
                StudentId = 1001,
                Studentrollno=1,
                Name = "Mann",
                Gender = "Male",
                City = "Delhi"
            };
            return View(student);
        }
	}
}

View:

@model MVC_Tutorial_Display_Student_Information.Models.Student

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>MVC Tutorial Display Student Information</title>
<style type="text/css">
body {
width: 980px;
margin: 0px auto;
text-align: center;
padding-top: 50px;
font-size: 20px;
}
</style>
</head>
<body>
<div>
<table style="font-family:Arial">

<tr>
<td>Student ID:</td><td>@Model.StudentId</td>
</tr>

<tr>
<td>Student Roll no.:</td><td>@Model.Studentrollno</td>
</tr>

<tr>
<td>Name:</td><td>@Model.Name</td>
</tr>

<tr>
<td>Gender:</td><td>@Model.Gender</td>
</tr>

<tr>
<td>City:</td><td>@Model.City</td>
</tr>

</table> 
</div>
</body>
</html>