Почему представление не может отображать данные студентов на стороне клиента, а это ASP. NET MVC? - PullRequest
0 голосов
/ 08 мая 2020

Во-первых, это классы моделей:

public class Student
{
    public int ID;
    public string Name;
    public string RollNo;
    public string Branch;
    public string Course;
    public string Year;
}

public class StudentData
{
    public IEnumerable<Student> Data()
    {
        List<Student> StudentsList = new List<Student>();

        Student[] SArr = new Student[4];
        SArr[0].ID = 1;
        SArr[0].Name = "Narender";
        SArr[0].RollNo = "0177EC161067";
        SArr[0].Branch = "EC";
        SArr[0].Course = "B.E";
        SArr[0].Year = "IV";

        StudentsList.Add(SArr[0]);

        SArr[1].ID = 2;
        SArr[1].Name = "Sandeep";
        SArr[1].RollNo = "0177CS161001";
        SArr[1].Branch = "CSE";
        SArr[1].Course = "B.E";
        SArr[1].Year = "IV";

        StudentsList.Add(SArr[1]);
        return StudentsList;
    }
}

А это контроллер:

public class MainController : Controller
{
    public ActionResult Index()
    {
        StudentData students = new StudentData();
        IEnumerable<Student> s = students.Data();

        return View(s.ToList());
    }
}

Вид такой:

@model IEnumerable<FirstApp.Models.Student>
<table width="400">
<th>ID</th>
<th>Name</th>
<th>Roll Number</th>
<th>Branch</th>
<th>Course</th>
<th>Year</th>
@foreach (var s in Model)
    {
        <tr>
            <td>@s.ID</td>
            <td>@s.Name</td>
            <td>@s.RollNo</td>
            <td>@s.Branch</td>
            <td>@s.Course</td>
            <td>@s.Year</td>
        </tr>``
    }
</table>

Получаю ошибка:

Ссылка на объект не указана для экземпляра объекта.

Описание: необработанное исключение произошло во время выполнения текущего веб-запроса. Просмотрите трассировку стека для получения дополнительной информации об ошибке и ее происхождении в коде.

Сведения об исключении: System.NullReferenceException: ссылка на объект не установлена ​​на экземпляр объекта.

Источник Ошибка:

Строка 26: SArr [0] .ID = 1;

Ответы [ 2 ]

0 голосов
/ 08 мая 2020

Не используйте массив и список.

Обновите класс вашей модели как таковой:

using System;
using System.Collections.Generic;
namespace FirstApp.Models
{
public class Student
{
    public int ID;
    public string Name;
    public string RollNo;
    public string Branch;
    public string Course;
    public string Year;
}
public class StudentData
{
    public IEnumerable<Student> Data()
    {
        var StudentsList = new List<Student>()
        {
           new Student(){
            ID = 1,
            Name = "Narender",
            RollNo = "0177EC161067",
            Branch = "EC",
            Course = "B.E",
            Year = "IV"
           },
           new Student(){
            ID = 2,
            Name = "Sandeep",
            RollNo = "0177CS161001",
            Branch = "CSE",
            Course = "B.E",
            Year = "IV"
           }
        };
        return StudentsList;
    }
}
}
0 голосов
/ 08 мая 2020

Вот обновленный вид

@using FirstApp.Models
@model IEnumerable<Student>
<table width="400">
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Roll Number</th>
        <th>Branch</th>
        <th>Course</th>
        <th>Year</th>
    </tr>
    @foreach (var s in Model)
    {
        <tr>
            <td>@s.ID</td>
            <td>@s.Name</td>
            <td>@s.RollNo</td>
            <td>@s.Branch</td>
            <td>@s.Course</td>
            <td>@s.Year</td>
        </tr>
    }
</table>

Обновленные модели будут:

public class Student
    {
        public int ID;
        public string Name;
        public string RollNo;
        public string Branch;
        public string Course;
        public string Year;
    }

    public class StudentData
    {
        public IEnumerable<Student> Data()
        {
            return new List<Student>{
                new Student{
                ID = 1,
                Name = "Narender",
                RollNo = "0177EC161067",
                Branch = "EC",
                Course = "B.E",
                Year = "IV"
                },
                new Student{
                    ID = 2,Name = "Sandeep",
                    RollNo = "0177CS161001",
                    Branch = "CSE",
                    Course = "B.E",
                    Year = "IV"
                }
            };
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...