asp.net 页面不显示数据库内容(MVC)

kx1ctssn  于 2023-02-17  发布在  .NET
关注(0)|答案(1)|浏览(157)

我在我的项目中有一个视图,它包含了一个动态模型的其他模型,问题是页面加载,但页面保持空白,即使数据库有条目,在打开页面时有四个错误(2种类型的错误):

Using the generic type 'IEnumerable<T> requires 1 type argument'
//and
The non-generic type 'ConcertViewModel' cannot be used with type parameters

我的视图模型:

public class ConcertViewModel
{
    public IEnumerable<ClassicalConcert> ClassicalConcerts { get; set; }
    public IEnumerable<RegularConcert> RegularConcerts { get; set; }
    public IEnumerable<Party> Parties { get; set; }
}

我的控制者:

public class ConcertController : Controller
    {
        private readonly AppDbContext _context;
        public ConcertController(AppDbContext context)
        {
             _context = context;
        }
        public IActionResult Concerts()
        {
            ConcertViewModel allConcerts = new ConcertViewModel();
            allConcerts.RegularConcerts = _context.RegularConcerts;
            allConcerts.ClassicalConcerts = _context.ClassicalConcerts;
            allConcerts.Parties = _context.Parties;
            return View(allConcerts);
        }
    }

我的观点:

@model ConcertViewModel
@{
    ViewData["Title"] = "Concerts";
}


<h1>Concerts</h1>

<div class="row">
    <div class="col-md-8 offset-md-2">
        <table>
            <thead> 
                <tr>
                    
                </tr>
            </thead>
            <tbody> 
                @foreach (ClassicalConcert classicalConcert in Model.ClassicalConcerts)
                {
                    <tr>
                        <td class="align-middle">
                            <img class="rounded-circle" src ="@classicalConcert.ImageURL" alt="@classicalConcert.PerformerName" style="max-width: 150px"></img>
                        </td>
                        <td class="align-middle">
                            @Html.DisplayFor(model => classicalConcert.PerformerName)
                        </td>
                        <td class="align-middle">
                            @Html.DisplayFor(model => classicalConcert.TicketsCount)
                        </td>
                        <td class="align-middle">
                            @Html.DisplayFor(model => classicalConcert.PerformanceDate)
                        </td>
                        <td class="align-middle">
                            @Html.DisplayFor(model => classicalConcert.Location)
                        </td>
                        <td class="align-middle">
                            @Html.DisplayFor(model => classicalConcert.Description)
                        </td>
                        <td class="align-middle">
                            @Html.DisplayFor(model => classicalConcert.VoiceType)
                        </td>
                        <td class="align-middle">
                            @Html.DisplayFor(model => classicalConcert.ConcertName)
                        </td>
                        <td class="align-middle">
                            @Html.DisplayFor(model => classicalConcert.ComposersName)
                        </td>
                        <td class="align-middle"> 
                            <a class="btn btn-outline-primary"><i class="bi bi-pencil-square"></i>Edit</a>|
                            <a class="btn btn-outline-info"><i class="bi bi-eye"></i>Details</a>|
                            <a class="btn btn-danger"><i class="bi bi-trash"></i>Delete</a>
                        </td>
                    </tr>
                    @foreach(Party party in Model.Parties)
                    {
                        <tr>
                            <td class="align-middle">
                                <img class="rounded-circle" src="@party.ImageURL" alt="@party.PerformerName" style="max-width: 150px"></img>
                            <td class="align-middle">
                                    @Html.DisplayFor(model => party.PerformerName)
                            </td>
                            <td class="align-middle">
                                    @Html.DisplayFor(model => party.TicketsCount)
                            </td>
                            <td class="align-middle">
                                    @Html.DisplayFor(model => party.PerformanceDate)
                            </td>
                            <td class="align-middle">
                                    @Html.DisplayFor(model => party.Location)
                            </td>
                            <td class="align-middle">
                                    @Html.DisplayFor(model => party.Description)
                            </td>
                            <td class=" align-middle">
                                    @Html.DisplayFor(model => party.AgeLimit)
                            </td>
                        </tr>
                    }
                    @foreach (RegularConcert regular in Model.RegularConcerts)
                    {
                        <tr>
                            <td class="align-middle">
                                <img class="rounded-circle" src="@regular.ImageURL" alt="@regular.PerformerName" style="max-width: 150px"></img>
                            <td class="align-middle">
                                    @Html.DisplayFor(model => regular.PerformerName)
                            </td>
                            <td class="align-middle">
                                    @Html.DisplayFor(model => regular.TicketsCount)
                            </td>
                            <td class="align-middle">
                                    @Html.DisplayFor(model => regular.PerformanceDate)
                            </td>
                            <td class="align-middle">
                                    @Html.DisplayFor(model => regular.Location)
                            </td>
                            <td class="align-middle">
                                    @Html.DisplayFor(model => regular.Description)
                            </td>
                        </tr>
                    }
                }
            </tbody>
            <tr>
                <td>

                </td>
            </tr>
        </table>
    </div>
</div>

这个错误甚至没有出现在我编写的代码中。

    • 编辑:**

我今天回到项目中,错误已经消失,但它仍然无法从数据库中获取数据
我检查了数据库,至少有一个交易方应该显示在页面上

    • 错误图像:**

agxfikkp

agxfikkp1#

解决方案很简单,在我的数据库中有三个单独的模型分别对应于每个模型,我有一个类型党的单一数据库记录,我没有填写其他模型,所以所有的模型返回空,我修复了它,只是添加了一些其他类型的记录(常规音乐会,古典音乐会)到数据库中,网站开始像一个魅力。

相关问题