使用Razor页面在表格中显示数据时出现问题

时间:2019-04-27 06:58:56

标签: c# razor model-view-controller

我正在尝试从使用MVC模型切换到Razor Pages,但是我无法将数据库中的数据显示到表中。问题是,在Razor页面上,我无法使用foreach,因为模型中没有GetEnumerator,并且在调用页面时使用@model IEnumerable<...>会引发异常,说明该类型“ ModelName”没有构造函数,即使如果在MVC中可以使用。

在MVC中,我是这样做的,而且一切正常:

控制器

public IActionResult ViewMenu()
{
    IList<ServixModel.MenuModel> menu = null;
    using (_context)
    {
        menu = _context.Menu.Select(m => new ServixModel.MenuModel()
        {
            Id = m.Id,
            Id_Parent = m.Id_Parent,
            Testo = m.Testo,
            Link = m.Link,
            SortOrder = m.SortOrder
        }).ToList<ServixModel.MenuModel>();
    }
    return View(menu);
}

查看

@model IEnumerable<servix_test1.Models.ServixModel.MenuModel>

@{
    ViewData["Title"] = "ViewMenu";
}

<h1>ViewMenu</h1>

<table style="border:solid 1px black;">

    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Id)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Id_Parent)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Testo)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Link)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SortOrder)
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Id_Parent)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Testo)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Link)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SortOrder)
            </td>
        </tr>
    }

</table>


使用剃须刀页面我必须对其进行一些更改:

ViewMenu.cshtml

@page
@model servix2.Models.Servix2Model.MenuModel
@{
    ViewData["Title"] = "ViewMenu";
    Layout = "~/Pages/Shared/_Layout.cshtml";
}
<h1>ViewMenu</h1>

<table>
    <thead>
        @Html.DisplayNameFor(model => model)
    </thead>
    <thead>
        @Html.DisplayNameFor(model => model.Id_Parent)
    </thead>
    <thead>
        @Html.DisplayNameFor(model => model.Link)
    </thead>
    <thead>
        @Html.DisplayNameFor(model => model.SortOrder)
    </thead>
    <thead>
        @Html.DisplayNameFor(model => model.Testo)
    </thead>
    @foreach (var item in Model) //Error here
    {
        <tr>
            <td>
                @Html.DisplayNameFor(modelItem => item.Id)
            </td>
            <td>
                @Html.DisplayNameFor(modelItem => item.Id_Parent)
            </td>
            <td>
                @Html.DisplayNameFor(modelItem => item.Link)
            </td>
            <td>
                @Html.DisplayNameFor(modelItem => item.SortOrder)
            </td>
            <td>
                @Html.DisplayNameFor(modelItem => item.Testo)
            </td>

        </tr>
    }
</table>

ViewMenu.cshtml.cs

    public class ViewMenuModel : PageModel
    {

        private readonly Servix2DbContext _context;  

        public ViewMenuModel(Servix2DbContext context)
        {
            _context = context;
        }

        public IList<MenuModel> Menu { get; set; }

        public async Task OnGetAsync()
        {
            Menu =  await _context.Menu.AsNoTracking().ToListAsync();
        }
    }

如果您认为代码编写不正确,并且我可以改进某些地方,请告诉我。

感谢愿意提供帮助的人。

1 个答案:

答案 0 :(得分:0)

我设法像这样修复它:

Viewmenu.cshtml

@page
@model servix2.Pages.ViewMenuModel
@{
    ViewData["Title"] = "ViewMenu";
    Layout = "~/Pages/Shared/_Layout.cshtml";
}
<h1>ViewMenu</h1>

<table>
    <tr>
        <th>@Html.DisplayNameFor(x => x.Menu[0].Id)</th>        
        <th>@Html.DisplayNameFor(x => x.Menu[0].Id_Parent)</th>
        <th>@Html.DisplayNameFor(x => x.Menu[0].Link)</th>
        <th>@Html.DisplayNameFor(x => x.Menu[0].Testo)</th>
        <th>@Html.DisplayNameFor(x => x.Menu[0].SortOrder)</th>
    </tr>
    @foreach (var item in Model.Menu)
    {
        <tr>
            <td>@item.Id</td>
            <td>@item.Id_Parent</td>
            <td>@item.Link</td>
            <td>@item.Testo</td>
            <td>@item.SortOrder</td>
        </tr>
    }
</table>

ViewMenu.cshtml.cs

public class ViewMenuModel : PageModel
{

    private readonly Servix2DbContext _context;  

    public ViewMenuModel(Servix2DbContext context)
    {
        _context = context;
    }

    public List<MenuModel> Menu { get; set; }

    public async Task OnGetAsync()
    {
        Menu =  await _context.Menu.AsNoTracking().ToListAsync();
    }
}