ASP.Net MVC,ViewPage<动态>和 EditorFor/LabelFor

发布于 2024-09-19 04:33:45 字数 537 浏览 6 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒解孤独 2024-09-26 04:33:45

不要使用ViewPage。我建议您使用视图模型,并将您的视图强式键入到此视图模型:

var model = new MyViewModel
{
    User = new User
    {
        Name = "foo"
    },
    SomeOtherProperty = "bar"
};
return View(model);

然后将您的视图强式键入到 ViewPage 并且:

@Html.LabelFor(x => x.User.Name)
@Html.EditorFor(x => x.User.Name)
<div>@Model.SomeOtherProperty</div>

Don't use ViewPage<Dynamic>. I would recommend you using a view model and strongly type your view to this view model:

var model = new MyViewModel
{
    User = new User
    {
        Name = "foo"
    },
    SomeOtherProperty = "bar"
};
return View(model);

and then strongly type your view to ViewPage<MyViewModel> and:

@Html.LabelFor(x => x.User.Name)
@Html.EditorFor(x => x.User.Name)
<div>@Model.SomeOtherProperty</div>
夏花。依旧 2024-09-26 04:33:45

看起来表达式 trees => http://msdn.microsoft.com/en-us/library/bb397951.aspx 不得包含任何动态变量。

不幸的是,当您在其中使用动力学时,TModel 就是这种情况。

public static MvcHtmlString TextBoxFor<TModel, TProperty>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func> expression
)

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.

public static MvcHtmlString TextBoxFor<TModel, TProperty>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func> expression
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文