简体   繁体   中英

ASP.NET MVC - Access Issue in DropDownListFor

I'm making a page. There is a table on the page. My goal is to Filter the table system. I will do the filtering with Class.ClassName .

My codes:

Index.cshtml :

@model List<StudentApp.Models.Entity.StudentTable>
@{
    ViewData["Title"] = "Manage Student";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<br />
<h1>Manage Student</h1>
<br />
@using (Html.BeginForm("Index", "StudentTable", FormMethod.Get))
{
    <p>
         @Html.DropDownListFor(m => m., (List<SelectListItem>)ViewBag.ClassT, new { @class = "form-control"}),
        <b>Student Name:</b> @Html.TextBox("p");
        <input type="submit" value="Ara">
    </p>
}
<table id="tbl1" class="table table-bordered">
    <tr>
        <th>
            Student ID
        </th>
        <th>
            Student Name
        </th>
        <th>
            Student Class
        </th>
        <th>
            Edit
        </th>
        <th>
            Delete
        </th>
    </tr>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @item.Id
                </td>
                <td>
                    @item.Name
                </td>
                <td>
                    @item.Class.ClassName
                </td>
                <td>
                    <a href="/StudentTable/EditStudent/@item.Id" class="btn btn-success">Edit</a>
                </td>
                <td>
                    <a href="/StudentTable/Delete/@item.Id" class="btn btn-danger">Delete</a>
                </td>
            </tr>
        }
    </tbody>
</table>

<a href="/StudentTable/AddStudent"  class="btn btn-primary">Add Student</a>

Controller :

StudentDatabaseContext db = new StudentDatabaseContext();
public IActionResult Index(string p)
{
    var studentList = db.StudentTables.Include(x => x.Class).ToList();
    var degerler = from d in db.StudentTables select d;
    if (!string.IsNullOrEmpty(p))
    {
        degerler = degerler.Where(m => m.Class.ClassName.Contains(p));
    }
    return View(degerler.ToList());
}

Model :

using System;
using System.Collections.Generic;

namespace StudentApp.Models.Entity
{
    public partial class StudentTable
    {
        public int Id { get; set; }
        public string? Name { get; set; }
        public int ClassId { get; set; }

        public virtual ClassTable? Class { get; set; }
    }
}

And I'm having a problem like this:

在此处输入图像描述

As you can see in the picture, I cannot access the data named Class.ClassName . My goal is to list items in DropDownListFor. Why could this be? How can I solve it? Thanks for help.

In the controller. Add This

ViewBag.ClassT = new SelectList((from s in db.Class.OrderBy(a => a.Code)
                                          select new { ID = s.Id, 
                                           FullDescription = s.Code }),
                                         "ID", "FullDescription");

In the View

@Html.DropDownListFor(m => m.ClassId, (SelectList)ViewBag.ClassT,"-Select-",new { @class = "form-control})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM