我一直在玩ASP.NET MVC,并有一个问题。或者,也许是我担心我做错了。只是在一个蹩脚的网站上工作,稍微拉伸我的翅膀。我很抱歉,这个问题并不简明。ASP.Net MVC和RenderPartial w /相对路径
好的,这是场景。当用户访问家庭/索引时,页面应显示产品列表和文章列表。文件布局是这样(DAL是我数据访问层):但是
Controllers Home Index Views Home Index inherits from ViewPage Product List inherits from ViewUserControl<IEnumerable<DAL.Product>> Single inherits from ViewUserControl<DAL.Product> Article List inherits from ViewUserControl<IEnumerable<DAL.Article>> Single inherits from 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",...)
的电话抱怨说没有找到“单一”视图。该错误指示:
The partial view 'Single' could not be found. The following locations were searched: ~/Views/Home/Single.aspx ~/Views/Home/Single.ascx ~/Views/Shared/Single.aspx ~/Views/Shared/Single.ascx
因为我打电话的RenderAction()从产品的图,我预期运行时寻找视图中的“单”视图\产品。但似乎查找是相对于调用原始视图(/ Controller/Home invoked/Views/Product)而不是当前视图的控制器。
所以我能够改变浏览\产品来解决这个问题,使得:
View.Product.List will call
foreach(Product product in View.Model)
Html.RenderPartial("~/Views/Product/Single.ascx", product);
,而不是
View.Product.List will call
foreach(Product product in View.Model)
Html.RenderPartial("Single", product);
此修复工作,但..我不明白为什么我需要指定视图的完整路径。对我来说,将相对名称解释为相对于当前视图的路径而不是原始控制器的视图路径是有意义的。我想不出任何有用的情况下,解释名称相对于控制器的视图,而不是当前视图是有用的(除了在典型的情况下,他们是相同的)。
在这段时间我应该有一个问号?强调这实际上是一个问题。
跨越这只是偶然寻找相关的东西(我知道它的6个月以上)。但是你提到使用RenderAction(),但是你所有的例子都使用RenderPartial(),这是一个错字吗?我会假设RenderAction(而不是RenderPartial)应该在这种情况下工作? – 2009-08-05 21:33:07