将 ViewUserControl 上的属性绑定到 .NET MVC2 中的本地变量
我想做这样的事情,其中 item 是 .aspx 页面中的本地变量:
<p:ProgressBar runat="server" Progress="<%#item.Completed/item.Total%>" Width="100" />
绑定表达式未检测本地页面级别变量。有没有办法可以使用 RenderPartial 来不完成此任务?
I want to do something like this where item is a local variable in the .aspx page:
<p:ProgressBar runat="server" Progress="<%#item.Completed/item.Total%>" Width="100" />
the binding expression isn't detecting the local page level variables. Is there a way I can accomplish this wihtout using RenderPartial?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应在 ASP.NET MVC 应用程序中使用任何服务器端控件 (
runat="server"
),因为它们依赖于 ViewState 和 PostBack,而这些概念在 ASP.NET MVC 中已不再存在。唯一的例外是 Webforms 视图引擎使用
面板来实现母版页。也没有绑定的概念。在 ASP.NET MVC 中,您有一个模型、一个控制器和一个视图。控制器填充一些模型并将其传递给要显示的视图。视图本身可以使用 HTML 助手 生成简单的标记或包含更复杂场景的其他部分视图。
因此,您从定义一个模型开始:
然后您有一个控制器将操纵该视图模型:
最后您将拥有该模型的强类型视图,您将在其中显示标记:
You shouldn't use any server side controls (
runat="server"
) in an ASP.NET MVC application because that they rely on ViewState and PostBack which are notions that no longer exist in ASP.NET MVC. The only exception makes<asp:Content>
panels used by the webforms view engine to implement master pages. Also there is no notion of binding.In ASP.NET MVC you have a model, a controller and a view. The controller populates some model and passes it to the view to be shown. The view itself could use HTML helpers to generate simple markup or include other partial views for more complex scenarios.
So you start with defining a model:
then you have a controller which will manipulate this view model:
and finally you would have a strongly typed view to this model where you would show the markup:
答案是确实没有一个好的方法来做到这一点。您可以使用 ViewDataKey 属性传入模型,但是您必须使用当前 ViewDictionary 中存在的键,因此您将通过此路线向父视图添加一些内容。
如果您想设置仅供查看的属性,可能可以使用自定义 HTML 帮助程序,但最好尝试找到达林提到的解决方法。
The answer to this is that there really is not a good way to do this. You can pass in the model using the ViewDataKey properties, but you're stuck using keys that exist in the current ViewDictionary so you're going to add some cruft to the parent view by going this route.
If you want to set view-only properties, it may be possible using a custom HTML helper but probably just better off trying to find a work around as Darin alludes to.