我一直在玩ASP.NET MVC并且有一个问题。或者也许我担心我这样做错了。只是在一个跛足的地方工作,伸展我的翅膀。对不起,这个问题根本不简洁。
好的,这是场景。当用户访问home / index时,页面应显示产品列表和文章列表。文件布局是这样的(DAL是我的数据访问层):
控制器 家 指数 查看 家 索引继承自ViewPage 产品 List继承自ViewUserControl <IEnumerable <DAL.Product >> 单个继承自ViewUserControl <DAL.Product> 文章 List继承自ViewUserControl <IEnumerable <DAL.Article >> 单个继承自ViewUserControl <DAL.Article>
Controllers.HomeController.Index produces a View whose ViewData contains two entries, a IEnumerable<DAL.Product> and a IEnumerable<DAL.Article>.
View.Home.Index will use those view entries to call:
Html.RenderPartial("~/Views/Product/List.ascx", ViewData["ProductList"])
and Html.RenderPartial("~/Views/Article/List.ascx", ViewData["ArticleList"])
View.Product.List will call
foreach(Product product in View.Model)
Html.RenderPartial("Single", product);
View.Article.List does something similar to View.Product.List
然而,这种方法失败了这种方法对我来说很有意义,但也许对这些MVC平台有更多经验的人会认识到更好的方法。
以上在View.Product.List中产生错误。打电话给 Html.RenderPartial("Single",...)
抱怨没有找到“单一”视图。错误表明:
无法找到部分视图“单个”。搜索了以下位置: 〜/浏览/首页/ Single.aspx 〜/浏览/首页/ Single.ascx 〜/查看/共享/ Single.aspx 〜/查看/共享/ Single.ascx
因为我从Product中的视图调用RenderAction(),所以我希望运行时在Views \ Product中查找“Single”视图。然而,似乎查找是相对于调用原始视图(/ Controller / Home invoked / Views / Product)而不是当前视图的控制器。
所以我可以通过更改Views \ Product来解决这个问题,这样:
View.Product.List will call
foreach(Product product in View.Model)
Html.RenderPartial(“〜/查看/产品/ Single.ascx”, product);
代替
View.Product.List will call
foreach(Product product in View.Model)
Html.RenderPartial(“单”, product);
这个修复工作,但..我不明白为什么我需要指定视图的完整路径。对于我来说,相对于当前视图的路径而不是原始控制器的视图路径来解释相对名称是有意义的。我想不出任何有用的情况,即相对于控制器的视图而不是当前视图解释名称是有用的(除了在它们相同的典型情况下)。
大约在这个时候我应该有一个问号?强调这实际上是一个问题。