gpt4 book ai didi

c# - 带列表的 ASP.NET MVC PartialView

转载 作者:太空狗 更新时间:2023-10-29 22:37:29 24 4
gpt4 key购买 nike

我正在处理我的第一个 MVC 项目。我想创建一个带有标题的页面,并在此标题中放置一个带有类别列表的局部 View 。

这是我目前所做的:我创建了母版页 (_Home.cshtml)。比在共享文件夹中我创建了一个 View (Category.cshtml)。看我的照片。 enter image description here

我的 Category.cshtml 内容:

@model IEnumerable<ArtSchool.Models.Category>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Visible)
</th>
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Visible)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>

我的母版页文件:

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>_Home</title>
</head>
<body>
<div>

@Html.Partial("ASCX/Header")

@Html.Partial("Category")

@RenderBody()
</div>

当我运行项目时出现错误:

enter image description here

我知道这是一个新手问题,但这是我的第一个 MVC 项目。谢谢!

最佳答案

解决方案一

如果你想使用部分 View ,你需要通过这种方式将模型传递给这个助手

@Html.Partial("Category", CategoryModel)

在传递此模型之前,您必须用一些数据填充它。

方案二

您也可以使用 @Html.Action() 方法和 ActionResult 方法的名称,它将为您返回部分 View 。

例如:

  @Html.Action("GetCategories", "ControllerName")

public ActionResult GetCategories() {
// fill some data for your model here
return PartialView("Category", model);
}

关于c# - 带列表的 ASP.NET MVC PartialView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21729505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com