gpt4 book ai didi

asp.net-mvc - 带有相对路径的 ASP.Net MVC 和 RenderPartial

转载 作者:行者123 更新时间:2023-12-04 02:09:01 26 4
gpt4 key购买 nike

我一直在玩 ASP.NET MVC 并且有一个问题。或者也许它担心我做错了。只是在一个蹩脚的网站上工作以拉伸(stretch)我的翅膀。很抱歉,这个问题一点也不简洁。

好的,这是场景。当用户访问主页/索引时,页面应显示产品列表和文章列表。文件布局是这样的(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",...)提示找不到“单一” View 。该错误表明:
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

Because I was calling RenderAction() from a view in Product, I expected the runtime to look for the "Single" view within Views\Product. It seems however the lookup is relative the controller which invoked the original view (/Controller/Home invoked /Views/Product) rather than the current view.

So I am able to fix this by changing Views\Product, such that:

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);

此修复程序有效,但是.. 我不明白为什么我需要指定 View 的完整路径。对我来说,相对于当前 View 的路径而不是原始 Controller 的 View 路径来解释相对名称是有意义的。我想不出任何有用的情况来解释相对于 Controller View 而不是当前 View 的名称是有用的(除了在它们相同的典型情况下)。

这个时候我应该打个问号吧?强调这一点实际上是一个问题。

最佳答案

Because I was calling RenderAction()from a view in Product

...

I do not understand why I neededto specify the full path of the view.It would make sense to me for therelative name to be interpretedrelative to the current view's pathrather than the original controller'sview path


我认为您误解的部分是“执行地点”,因为没有更好的或官方的术语。路径与您的 View 无关,甚至与您所说的“ Controller View ”无关。它们与定义 Controller 上下文的请求 URL 相关。我可能说得不太好,但是如果您在 Reflector 中花一点时间查看 URL 和路由是如何解析的,我想这一切都会在您的脑海中形成。

关于asp.net-mvc - 带有相对路径的 ASP.Net MVC 和 RenderPartial,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/277284/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com