如何获取控制器名称以及基本控制器中可用的数据?
我有 2 个问题。
首先,背景:
我有一个 PageController : Controller
和 SearchController : PageController
PageController 定义为:
public class PageController : Controller
{
protected ISite Site;
protected PageConfiguration PageConfiguration;
protected override void Initialize(RequestContext rc)
{
this.Site = new Site(SessionUser.Instance().SiteId);
this.PageConfiguration = this.Site.CurrentSite.GetPage(/* controller name */);
base.Initialize(rc);
}
}
我有 Site & PageConfiguration 存储在 PageController 中,因为每个实现 PageController 的页面都需要它们
问题 1:
我正在尝试从现有的 ASP.NET 应用程序移植它。需要页面名称才能获取 PageConfiguration 对象。我将使用控制器的名称来代替页面名称。对我来说将其作为字符串的最佳方法是什么?
问题2:
我正在尝试渲染依赖于该数据的 PartialView,如下所示:
<%@ Page Title=""
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<PageConfiguration>" %>
<%@ Import Namespace="tstFactsheetGenerator.Models.Panels"%>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% Html.RenderPartial("Search", new Search { PanelConfiguration = Model.Panels[0] }); %>
</asp:Content>
我可以看到,当调用 RenderPartial() 方法时,PageConfiguration 模型具有我需要的面板。但在 Search 类的构造函数中,PanelConfiguration 为 null。
有没有更好的&更有效的方式来提供页面所需的数据?谢谢。
I have 2 questions.
First, background:
I have a PageController : Controller
and SearchController : PageController
PageController is defined as:
public class PageController : Controller
{
protected ISite Site;
protected PageConfiguration PageConfiguration;
protected override void Initialize(RequestContext rc)
{
this.Site = new Site(SessionUser.Instance().SiteId);
this.PageConfiguration = this.Site.CurrentSite.GetPage(/* controller name */);
base.Initialize(rc);
}
}
I have Site & PageConfiguration stored in PageController because every page that implements PageController needs them
Question 1:
I'm trying to port this from an existing ASP.NET app. that requires a page name to get the PageConfiguration object. Instead of a page name, I'm going to use the Controller's name instead. What's the best way for me to get that as a string?
Question 2:
I'm trying to render a PartialView which relies on that data, as follows:
<%@ Page Title=""
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<PageConfiguration>" %>
<%@ Import Namespace="tstFactsheetGenerator.Models.Panels"%>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% Html.RenderPartial("Search", new Search { PanelConfiguration = Model.Panels[0] }); %>
</asp:Content>
I can see that when the RenderPartial() method is called the PageConfiguration Model has has the Panel I need. But in the constructor for the Search class that PanelConfiguration is null.
Is there a better & more effective way to make the data I need for the page available? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题 1:
问题 2:
当您使用类初始值设定项语法 (
new { PanelConfiguration = ... }
) 时,PanelConfiguration
在的构造函数中将为 null搜索
类但这重要吗?您不应该在那里使用PanelConfiguration
属性。在Search
部分中它将被初始化。如果您需要在构造函数中使用它(我不明白为什么您需要它),您可以将其添加为构造函数参数。Question 1:
Question 2:
As you are using class initializer syntax (
new { PanelConfiguration = ... }
) thePanelConfiguration
will be null in the constructor of theSearch
class but does it matter? You shouldn't be using thePanelConfiguration
property there. In theSearch
partial it will be initialized. If you need to use it in the constructor (I don't see why you would need it) you could add it as constructor argument.