【发布时间】:2016-04-11 21:02:19
【问题描述】:
我正在尝试将字符串列表返回到出现在我的 _Layout.cshtml 页面一侧的导航菜单。我可以很好地返回一个字符串,但我应该返回一个 PartialViewResult。这是我得到的错误:
{"未找到局部视图“菜单”或没有视图引擎支持搜索的位置。搜索了以下位置:\r\n~/Views/Nav/Menu.aspx\r\n~/Views/ Nav/Menu.ascx\r\n~/Views/Shared/Menu.aspx\r\n~/Views/Shared/Menu.ascx\r\n~/Views/Nav/Menu.cshtml\r\n~/ Views/Nav/Menu.vbhtml\r\n~/Views/Shared/Menu.cshtml\r\n~/Views/Shared/Menu.vbhtml"}
我的控制器代码:
using SportsStore.Domain.Abstract;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace SportsStore.WebUI.Controllers
{
public class NavController : Controller
{
private IProductRepository repository;
public NavController(IProductRepository repo)
{
repository = repo;
}
public PartialViewResult Menu()
{
IEnumerable<string> categories = repository.Products
.Select(x => x.Category)
.Distinct()
.OrderBy(x => x);
return PartialView(categories);
}
}
}
我的 _Layout.cshtml 文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<link href="~/Content/bootstrap-theme.css" rel="stylesheet" />
<title>@ViewBag.Title</title>
</head>
<body>
<div class="navbar navbar-inverse" role="navigation">
<a class="navbar-brand" href="#">SPORTS STORE</a>
</div>
<div class="row panel">
<div id="categories" class="col-xs-3">
@Html.Action("Menu", "Nav")
</div>
<div class="col-xs-8">
@RenderBody()
</div>
</div>
</body>
</html>
【问题讨论】:
标签: asp.net-mvc razor