动态类型化的ViewPage

动态类型化的ViewPage

本文介绍了动态类型化的ViewPage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能吗?下面是我尝试:

Is this possible? Here's what I'm trying:

    public ActionResult Index()
    {
        dynamic p = new { Name = "Test", Phone = "111-2222" };
        return View(p);
    }

然后我的观点从继承 System.Web.Mvc.ViewPage<动态方式> 并试图打印出Model.Name

And then my view inherits from System.Web.Mvc.ViewPage<dynamic> and tries to print out Model.Name.

我得到一个错误:'&LT;> f__AnonymousType1.Name'不可访问由于其保护级别

I'm getting an error: '<>f__AnonymousType1.Name' is inaccessible due to its protection level

因此​​,基本上,是什么,我试图做是不可能的?为什么或者为什么不呢?

So basically, is what I'm trying to do just not possible? Why or why not?

更新:这是我的看法。

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ...>
    <%=Model.Name%>
    <%=Model.Phone%>
</asp:Content>

观构造是内置在框架上。

The View constructor is built-in to the framework.

推荐答案

匿名类型不能用一种方法予以退货;它们只在定义它们的方法的范围内有效。

Anonymous types cannot be returned by a method; they are only valid within the scope of the method in which they are defined.

您应该使用,你有previously定义的模型类,并传递给你的视图。没有什么错传递不具有每个字段定义的模型类。

You should use a Model class that you have previously defined and pass that to your View. There is nothing wrong with passing a Model class that does not have every field defined.

更新

我想我是错了。这应该工作。也许问题是视图中。你能发布更多code?特别是查看它的构造。

I think I was wrong before. This should work. Perhaps the problem is within the View. Can you post more code? Especially the View and its constructor.

更新第二:

好吧,我错了传递一个匿名类型的另一种方法用作动态变量 - 是可以做的。

Ok, I was wrong about passing an anonymous type to another method for use as a dynamic variable -- that can be done.

但我也是错误的,我认为你正在试图做的工作会是什么。不幸的是,你不会。问题是,你正在使用的ViewPage&LT;的TModel&GT; ,它使用了的ViewDataDictionary&LT;的TModel&GT; 内部。因为他们需要强大的类型,您将无法使用动态对象和他们在一起。内部结构就是不使用动态内部,并指定为动态类型的失败。

But I was also wrong in my belief that what you're trying to do would work. Unfortunately for you, it will not. The problem is that you are using ViewPage<TModel>, which uses a ViewDataDictionary<TModel> internally. Because they require strong types, you won't be able to use dynamic objects with them. The internal structure just doesn't use dynamic internally, and specifying dynamic as the type fails.

需要怎么做才能是 DynamicViewPage 类和相应的 DynamicViewDataDictionary 类,它接受对象,并在内部存储它作为一个动态的。然后,你可以使用一个匿名类型,并把它传递给你的意见。

What would be needed is a DynamicViewPage class and corresponding DynamicViewDataDictionary class that accept object and store it internally as a dynamic. Then you could use an anonymous type and pass it to your Views.

这是说,你不会得到任何东西。你就可以指定你的意见,你所做的(即&LT;%= Model.Name%GT; ),但你不会从强类型中受益。就没有IntelliSense和就没有类型安全。 的ViewDataDictionary @Dennis帕尔默表明你做的一样好使用非类型化。

That said, you would not gain anything. You would be able to specify your Views as you have done (i.e. <%=Model.Name%>), but you would not benefit from strong typing. There would be no intellisense and there would be no type safety. You'd do just as well to use the untyped ViewDataDictionary as @Dennis Palmer suggests.

这是一个有趣的(并且,可惜对我来说,吸收)的思想实验,但我认为,最终,它不会发生。要么声明一个公共类型,并将它传递给你的意见,或使用无类型的字典。

This has been an interesting (and, unfortunately for me, absorbing) thought experiment, but I think, ultimately, that it's not going to happen. Either declare a public type and pass it to your Views, or use the untyped dictionary.

这篇关于动态类型化的ViewPage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 17:10