在 ASP.NET MVC 中封装用户控件
抱歉,如果这是一个基本问题 - 我在从页面框架过渡到 ASP.NET MVC 时遇到了一些麻烦。
在页面框架中,我经常使用 ASCX 文件来创建小的、封装的功能块,这些功能块包含在整个站点的各个位置。如果我正在构建一个页面并且需要其中一个控件 - 我只需添加一个引用,一切就可以正常工作。
据我所知,在 MVC 中,ASCX 文件只是一个部分视图。这是否意味着无论我想要添加这些功能单元之一,我还必须向控制器的操作方法添加一些代码,以确保相关的 ViewData 可用于 ASCX?
如果是这样的话,对我来说似乎有点倒退了。例如,这意味着我不能只是将控件“拖放到”母版页中,而不必向其视图使用该母版页的每个控制器添加代码!
我怀疑我错过了一些东西 - 任何帮助将不胜感激。
谢谢, - 克里斯
Sorry if this is a basic question - I'm having some trouble making the mental transition to ASP.NET MVC from the page framework.
In the page framework, I often use ASCX files to create small, encapsulated chunks of functionality which get inclded in various places throughout a site. If I'm building a page and I need one of these controls - I just add a reference and everything just works.
As far as I can tell, in MVC, the ASCX file is just a partial view. Does this mean that wherever I want to add one of these units of functionality I also have to add some code to the controller's action method to make sure the relevant ViewData is available to the ASCX?
If this is the case, it seems like a bit of a step backwards to me. It means, for example, that I couldn't just 'drop' a control into a master page without having to add code to every controller whose views use that master page!
I suspect I'm missing something - any help would be appreciated.
Thanks,
- Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的。
但是,您可以使用
RenderAction<您的视图中的 /code>
方法而不是
RenderPartial
,并且您的所有功能(包括传递给子视图的数据)都将被封装。换句话说,这将创建一个包含控制器方法、视图数据和部分视图的小包,可以从主视图中使用一行代码来调用它。
Yes.
However, you can use a
RenderAction
method in your view instead ofRenderPartial
, and all of your functionality (including the data being passed to the sub-view) will be encapsulated.In other words, this will create a little package that incorporates a controller method, view data, and a partial view, which can be called with one line of code from within your main view.
您的问题已经得到解答,但为了完整起见,有时您可能会发现另一种有吸引力的选择。
您是否见过 ASP.NET MVC 上如何屏蔽“控件”?它们是“HtmlHelper”的方法。例如,如果您想要一个绑定到“FirstName”的文本框,您可以这样做:
并且您可以为许多标准控件提供类似的东西。
你能做的就是创建你自己的方法。要创建自己的方法,您必须在 HtmlHelper 类上创建一个扩展方法,如下所示:
然后在您看来,打开包含此类定义的命名空间后,您可以像这样使用它
: ,这并不是特别有用。但你可以做非常有趣的事情。我经常使用的一种方法是采用枚举并使用该枚举中的值创建一个下拉列表(例如:
enum Gender { Male, Female }
,并且在视图中类似于性别:<%= Html.EnumDropDown(Model.Gender) %>
)。祝你好运!
Your question has been answered already, but just for sake of completeness, there's another option you might find attractive sometimes.
Have you seen how "controls" are masked on ASP.NET MVC? They are methods of the "HtmlHelper". If you want a textbox bound to "FirstName", for example, you can do:
And you have things like that for many standard controls.
What you can do is create your own methods like that. To create your own method, you have to create an extension method on the HtmlHelper class, like this:
Then in your view, after opening the namespace containing this class definition, you can use it like this:
Well, this is not particularly useful. But you can do very interesting things. One I use quite often is a method that takes an enumeration and created a Drop Down List with the values from this enumeration (ex:
enum Gender { Male, Female }
, and in the view something likeGender: <%= Html.EnumDropDown(Model.Gender) %>
).Good luck!
您可以渲染部分视图并将模型对象传递给。
在您的局部视图 (.ascx) 文件中,您可以使用“模型”对象(假设您在 @Control 减速中继承了正确的对象)对该对象执行您需要的任何操作。
当然,您可以不通过 Model,而只获取部分视图的文本并将其放置在您想要的位置。
在主视图(.aspx 文件)中,您需要在传递给分部视图的 ViewData 中定义正确的对象。
您可以执行的另一种方法是使用:
上一个方法的作用是调用控制器操作,获取其响应,并将其放置在您调用“RenderAction()”方法的位置。它相当于针对控制器操作运行请求并读取响应,只不过您将响应放在另一个文件中。
谷歌“renderaction 和 renderpartial”以获取更多信息。
You can render a partial view and pass a model object to.
In your partial view (.ascx) file, you can then use the "Model" object (assuming you've inherited the proper object in your @ Control deceleration) to do whatever you need to with that object.
You can, ofcourse, not pass and Model and just take the partial view's text and place it where you want it.
In your main view (.aspx file), you will need to define the proper object in the ViewData that you're passing to the partial view.
Another method you can do is use:
What the previous method does is call a controller Action, take its response, and place it where you called the "RenderAction()" method. Its the equivalent of running a request against a controller action and reading the response, except you place the response in another file.
Google "renderaction and renderpartial" for some more information.