0

为了解释,我的初始设置如下:

控制器:
Library => Controllers => ParametersController.cs
Library => Controllers => ReaderLevelController.cs
Library => Controllers => ResourceTypeController.cs

意见:
Library => Views => Parameters => Index.cshtml
Library => Views => ReaderLevel => Index.cshtml
Library => Views => ResourceType => Index.cshtml

每个视图都引用了相应的模型,如下所示:
@model IEnumerable<Library.DAL.PrmTbl_Level>

每个控制器都包含一个ActionResultfor Index(),一些FormCollections而不是其他。屏幕显示正常,拉取、编辑和更新数据库没有问题。

我想将我的视图更改为更具描述性的层次结构,因此像这样移动了我的视图文件:

意见:
Library => Views => Parameters => Index.cshtml
Library => Views => Parameters => ReaderLevel => Index.cshtml
Library => Views => Parameters => ResourceType => Index.cshtml

然后我更新了ParametersController.cs文件以反映现在将指向它的新 ActionResults,它是“父”文件:

    public ActionResult ResourceType() { return View("ResourceType/Index"); }

但是,现在新的“子”屏幕(在此示例中为 ReaderLevel 和 ResourceType)不会显示,因为当 foreach 循环尝试通过其相关模型运行时会引发错误 - 现在返回为 null。我对更改视图文件的位置如何改变数据访问的可行性感到困惑(因为在我看来,模型是通过@model IEnumerable<Library.DAL.PrmTbl_Level>不改变的 ab 路径填充的)。

有人可以解释更改视图的位置如何影响其对其控制器和模型的访问吗?

编辑
我当前的设置(文件结构如上):

参数控制器

public ActionResult Index() {
    ViewBag.Title = "Parameters";
    return View();
}
public ActionResult ResourceType() {
    return RedirectToAction("ResourceType");
}

这给了我适当的 url,但是“Firefox 检测到服务器正在以永远不会完成的方式重定向对该地址的请求。” 使用 RedirectToAction"ResourceType","Index"解析为 url '/Index/ResourceType' 并且找不到资源。

4

2 回答 2

0

So a few things I'm noticing here:

  1. In the action method you showed above you are not passing a Model to the View, you are simply calling the view. To pass the View a Model you need to supply it as the second argument to the View method, i.e. return View("ResourceType/Index", model);. Also I believe if you are passing a path, instead of just the name of the View, to the View method you need to specify the full path and file extension, i.e. ~/Views/Parameters/ResourceType/Index.cshtml. @Phil Sandler made a good point about using RedirectToAction as well since you are attempting to return a View associated with another Controller.

  2. If you want to change the default View hierarchy than you should consider extending the RazorViewEngine to supplement its search locations with your new directories. Have a look at this question for an example (it uses the WebFormViewEngine but you do the same thing for Razor): Can I specify a custom location to "search for views" in ASP.NET MVC?

  3. Lastly, I don't really understand your new hierarchy. Why are the ReaderLevel and ResourceType Views being placed under the Parameters subdirectory if they don't belong to that Controller? The new hierarchy actually seems more confusing which may make it harder to understand later. Maybe we don't have all the information to understand your decision but it seems odd.

于 2013-09-03T01:36:25.417 回答
0

我不清楚这行代码:

public ActionResult ResourceType() { return View("ResourceType/Index"); }

这是否意味着您正在从参数控制器加载资源类型视图?

如果是这样,那不是你想要做的。您想使用RedirectToAction

编辑:您还可以使用不同的 View() 重载在现有代码中提供模型。不过,我认为这不是您想要的,因为您确实有一个 ResourceTypeController。

编辑#2

根据您的评论,

然后我想,一旦加载了这些子视图,它们自己的单独控制器就会接管?

不,它实际上是相反的。控制器加载视图(并传递模型,如果需要)。看起来您正在尝试加载视图,并期望它加载自己的控制器。重定向到正确的 URL,控制器将接管。

于 2013-09-02T17:45:25.447 回答