POI*_*OIR 5 c# asp.net-mvc list partial-views
我是第一个MVC项目.我想创建一个带有标题的页面,并在此标题中放置一个带有类别列表的局部视图.
这就是我到目前为止所做的:我创建了母版页(_Home.cshtml).比在共享文件夹中我创建了一个View(Category.cshtml).看我的照片.

我的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>
Run Code Online (Sandbox Code Playgroud)
}
我的主页文件:
@{
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>
Run Code Online (Sandbox Code Playgroud)
当我运行项目时,我收到错误:

我知道这是一个新手问题,但这是我的第一个MVC项目.谢谢!
解决方案1
如果要使用局部视图,则需要以这种方式将模型传递给此帮助程序
@Html.Partial("Category", CategoryModel)
Run Code Online (Sandbox Code Playgroud)
在通过此模型之前,您必须填写一些数据.
解决方案2
你也可以使用@Html.Action()方法和ActionResult方法的名称,它将返回部分视图.
例如:
@Html.Action("GetCategories", "ControllerName")
public ActionResult GetCategories() {
// fill some data for your model here
return PartialView("Category", model);
}
Run Code Online (Sandbox Code Playgroud)
如果您想将这些部分解释为 HTML 中的一些静态部分,那么我建议您调用Html.Action()它返回您的部分:
@Html.Action("GetPageHeader","Home")
@Html.Action("GetPageCategories","Home")
Run Code Online (Sandbox Code Playgroud)
家庭控制器
[HttpGet]
public ActionResult GetPageHeader()
{
return PartialView(@"~/Views/Shared/_PageHeader.cshtml");
}
[HttpGet]
public ActionResult GetPageCategories()
{
var categories = databaseContext.GetAllCategories(); //Get your categs
return PartialView(@"~/Views/Shared/_Categories.cshtml",categories);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19637 次 |
| 最近记录: |