ASP.Net MVC,ViewPage<动态>和 EditorFor/LabelFor动态>
我正在使用 Razer 语法来玩 MVC3,尽管我相信这个问题更普遍。
在控制器中,我有类似的内容:
ViewModel.User = New User(); // The model I want to display/edit
ViewModel.SomeOtherProperty = someOtherValue; // Hense why need dynamic
Return View();
我的视图继承自 System.Web.Mvc.ViewPage
但如果我尝试执行以下操作:
<p>
@Html.LabelFor(x => x.User.Name
@Html.EditorFor(x => x.User.Name
</p>
我收到错误:“表达式树可能不包含动态操作”
但是,使用ViewPage 似乎很常见,EditorFor/LabelFor 也是如此。因此,如果没有办法做到这一点,我会感到惊讶 - 感谢任何指示。
I'm playing with MVC3 using the Razer syntax, though I believe the problem to be more general.
In the controller, I have something like:
ViewModel.User = New User(); // The model I want to display/edit
ViewModel.SomeOtherProperty = someOtherValue; // Hense why need dynamic
Return View();
My View inherits from System.Web.Mvc.ViewPage
But if I try to do something like:
<p>
@Html.LabelFor(x => x.User.Name
@Html.EditorFor(x => x.User.Name
</p>
I get the error: "An expression tree may not contain a dynamic operation"
However, the use of ViewPage seems quite common, as are EditorFor/LabelFor. Therefore I'd be surprised if there's not a way to do this - appreciate any pointers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用
ViewPage
。我建议您使用视图模型,并将您的视图强式键入到此视图模型:然后将您的视图强式键入到
ViewPage
并且:Don't use
ViewPage<Dynamic>
. I would recommend you using a view model and strongly type your view to this view model:and then strongly type your view to
ViewPage<MyViewModel>
and:看起来表达式 trees => http://msdn.microsoft.com/en-us/library/bb397951.aspx 不得包含任何动态变量。
不幸的是,当您在其中使用动力学时,TModel 就是这种情况。
It seems the expression trees => http://msdn.microsoft.com/en-us/library/bb397951.aspx must not contain any dynamic variables.
Unfortunately this is the case for TModel when you use dynamics in it.