我试图将字符串列表返回到_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〜/查看/导航/ Menu.cshtml \ r \ N〜/查看/导航/ Menu.vbhtml \ r \ N〜/查看/共享/ Menu.cshtml \ r \ N〜/查看/共享/ 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>
答案 0 :(得分:0)
您的菜单视图位于何处?
如果你没有明确定义你的View被调用的内容,MVC将主动并假设它的命名与你的Action相同,所以在这种情况下,你需要有一个Menu.cshtml
文件以下地点:
~/Views/Nav/Menu.cshml
~/Views/Shared/Menu.cshtml
或者如果它被命名为Menu.cshtml
以外的任何其他内容,您可能需要考虑在代码中指定:
// This assumes your partial view is named "_Menu.cshtml"
return PartialView("_Menu",categories);
答案 1 :(得分:0)
@model IEnumerable<string>
@Html.ActionLink("Home", "List", "Product", null,
new { @class = "btn btn-block btn-default btn-lg"})
@foreach (var link in Model)
{
@Html.RouteLink(link, new
{
controller = "Product",
action = "List",
category = link,
page = 1
}, new
{
@class = "btn btn-block btn-default btn-lg"
})
}